Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
NetPromoterScore
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
5 / 5
16
100.00% covered (success)
100.00%
1 / 1
 add
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getScore
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 countDetractors
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 countPassives
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 countPromoters
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Business\Marketing
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Business\Marketing;
16
17/**
18 * Net Promoter Score
19 *
20 * The net promoter score is a basic evaluation of the happiness of customers.
21 * Instead of customers the NPS can also be transferred to non-customers.
22 *
23 * @package phpOMS\Business\Marketing
24 * @license OMS License 2.0
25 * @link    https://jingga.app
26 * @since   1.0.0
27 */
28final class NetPromoterScore
29{
30    /**
31     * Score values
32     *
33     * @var int[]
34     * @since 1.0.0
35     */
36    private array $scores = [];
37
38    /**
39     * Add score.
40     *
41     * @param int $score Net promoter score
42     *
43     * @return void
44     *
45     * @since 1.0.0
46     */
47    public function add(int $score) : void
48    {
49        $this->scores[] = $score;
50    }
51
52    /**
53     * Get total NPS.
54     *
55     * Values of > 0 are considered good and above 50 is considered excellent.
56     * Remark: Amazon had a NPS of 69 in NA in 2016
57     *
58     * @latex NPS = Promoters - Detractors
59     *
60     * @return int Retunrs the NPS
61     *
62     * @since 1.0.0
63     */
64    public function getScore() : int
65    {
66        $promoters  = 0;
67        $passives   = 0;
68        $detractors = 0;
69
70        foreach ($this->scores as $score) {
71            if ($score > 8) {
72                ++$promoters;
73            } elseif ($score > 6) {
74                ++$passives;
75            } else {
76                ++$detractors;
77            }
78        }
79
80        $total = $promoters + $passives + $detractors;
81
82        return $total === 0 ? 0 : ((int) ($promoters * 100 / $total)) - ((int) ($detractors * 100 / $total));
83    }
84
85    /**
86     * Count detractors
87     *
88     * Detractors are all ratings below 7.
89     *
90     * @return int Returns the amount of detractors (>= 0)
91     *
92     * @since 1.0.0
93     */
94    public function countDetractors() : int
95    {
96        $count = 0;
97        foreach ($this->scores as $score) {
98            if ($score < 7) {
99                ++$count;
100            }
101        }
102
103        return $count;
104    }
105
106    /**
107     * Count passives
108     *
109     * Passives are all ratings between 7 and 8 (inclusive)
110     *
111     * @return int Returns the amount of passives (>= 0)
112     *
113     * @since 1.0.0
114     */
115    public function countPassives() : int
116    {
117        $count = 0;
118        foreach ($this->scores as $score) {
119            if ($score > 6 && $score < 9) {
120                ++$count;
121            }
122        }
123
124        return $count;
125    }
126
127    /**
128     * Count promoters
129     *
130     * Promotoers are all ratings larger 8
131     *
132     * @return int Returns the amount of promoters (>= 0)
133     *
134     * @since 1.0.0
135     */
136    public function countPromoters() : int
137    {
138        $count = 0;
139        foreach ($this->scores as $score) {
140            if ($score > 8) {
141                ++$count;
142            }
143        }
144
145        return $count;
146    }
147}