| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spiral\Statistics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Spiral\Statistics\Database\Sources\StatisticsSource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Spiral\Statistics\Database\Statistics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Track | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** @var StatisticsSource */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $source; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * Track constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @param StatisticsSource $statisticsSource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function __construct(StatisticsSource $statisticsSource) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->source = $statisticsSource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param string                  $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param float                   $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param \DateTimeInterface|null $datetime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function event(string $name, float $value, \DateTimeInterface $datetime = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $datetime = $this->datetime($datetime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $record = $this->source->findByEventNameAndDatetime($name, $datetime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         if (empty($record)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $this->addEvent($name, $value, $datetime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $this->updateEvent($record, $value); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param array                   $events Should be formatted as "event_name" => "event_value" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param \DateTimeInterface|null $datetime | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     public function events(array $events, \DateTimeInterface $datetime = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         //nothing to track | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         if (empty($events)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         foreach ($events as $name => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             $this->event($name, floatval($value), $datetime); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param \DateTimeInterface|null $datetime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return \DateTimeInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     protected function datetime(\DateTimeInterface $datetime = null): \DateTimeInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $datetime ?? new \DateTime('now'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function addEvent(string $name, float $value, \DateTimeInterface $datetime) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         /** @var Statistics $record */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $record = $this->source->create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $record->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $record->value = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $record->timestamp = $datetime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $record->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param Statistics $record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param float      $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     protected function updateEvent(Statistics $record, float $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $record->value += $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $record->save(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  | } | 
            
                        
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.