| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Oleg Krivtsov <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @date 12 October 2016 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright (c) 2016, Web Marketing ROI | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace WebMarketingROI\OptimizelyPHP\Resource\v2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use WebMarketingROI\OptimizelyPHP\Resource\v2\CampaignMetricResults; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Optimizely campaign results. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class CampaignResults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * The unique identifier for the Campaign | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var integer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $campaignId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * The significance level at which you would like to declare winning and  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * losing variations. A lower number minimizes the time needed to declare a  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * winning or losing variation, but increases the risk that your results  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * aren't true winners and losers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var number | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $confidenceThreshold; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * The latest time to count events in results | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var string  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $endTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * The breakdown of campaign results by metric. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var array[CampaignMetricResults] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $metrics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * The earliest time to count events in results | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private $startTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 52 | 3 | View Code Duplication |     public function __construct($options = array()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |         foreach ($options as $name=>$value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             switch ($name) {                 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  |                 case 'campaign_id': $this->setCampaignId($value); break; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  |                 case 'confidence_threshold': $this->setConfidenceThreshold($value); break; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 3 |  |                 case 'end_time': $this->setEndTime($value); break; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |                 case 'metrics': { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |                     $metrics = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |                     foreach ($value as $metricInfo) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |                         $metrics[] = new CampaignMetricResults($metricInfo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |                     $this->setMetrics($metrics);  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |                 case 'start_time': $this->setStartTime($value); break; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     throw new \Exception('Unknown option: ' . $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Returns this object as array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |     public function toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $options = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |             'campaign_id' => $this->getCampaignId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |             'confidence_threshold' => $this->getConfidenceThreshold(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |             'end_time' => $this->getEndTime(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |             'metrics' => array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             'start_time' => $this->getStartTime() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |         foreach ($this->getMetrics() as $metric) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |             $options['metrics'][] = $metric->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // Remove options with empty values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |         $cleanedOptions = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         foreach ($options as $name=>$value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |             if ($value!==null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |                 $cleanedOptions[$name] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |         return $cleanedOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |     public function getCampaignId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |         return $this->campaignId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 3 |  |     public function setCampaignId($campaignId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 3 |  |         $this->campaignId = $campaignId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |     public function getConfidenceThreshold() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 3 |  |         return $this->confidenceThreshold; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 116 | 3 |  |     public function setConfidenceThreshold($confidenceThreshold) | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 | 3 |  |         $this->confidenceThreshold = $confidenceThreshold; | 
            
                                                                        
                            
            
                                    
            
            
                | 119 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |     public function getEndTime() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |         return $this->endTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 3 |  |     public function setEndTime($endTime) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 3 |  |         $this->endTime = $endTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |     public function getMetrics() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |         return $this->metrics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 3 |  |     public function setMetrics($metrics) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 3 |  |         $this->metrics = $metrics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 3 |  |     public function getStartTime() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 3 |  |         return $this->startTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 3 |  |     public function setStartTime($startTime) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 3 |  |         $this->startTime = $startTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.