| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Ps2alerts\Api\Controller\Endpoint\Alerts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ps2alerts\Api\Controller\Endpoint\AbstractEndpointController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Ps2alerts\Api\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Ps2alerts\Api\Repository\AlertRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Ps2alerts\Api\Transformer\AlertTransformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class AlertEndpointController extends AbstractEndpointController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Construct | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param AlertRepository  $repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param AlertTransformer $transformer | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         AlertRepository  $repository, | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         AlertTransformer $transformer | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $this->repository  = $repository; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->transformer = $transformer; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Returns a single alert's information | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param  ServerRequestInterface  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param  ResponseInterface       $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param  array                   $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function getSingle(ServerRequestInterface $request, ResponseInterface $response, array $args) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $alert = $this->repository->readSingleById($args['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if (empty($alert)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             return $this->respondWithError('Alert not found', self::CODE_NOT_FOUND); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return $this->respond( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'item', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $alert, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $this->transformer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Returns all currently running alerts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param  ServerRequestInterface  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param  ResponseInterface       $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function getActives(ServerRequestInterface $request, ResponseInterface $response) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $actives = $this->repository->readAllByFields(['InProgress' => 1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return $this->respond( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'collection', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $actives, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $this->transformer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Returns all alerts in historical order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param  ServerRequestInterface  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param  ResponseInterface $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function getByDate(ServerRequestInterface $request, ResponseInterface $response) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $servers  = $this->validateQueryStringArguments($_GET['servers'], 'servers'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $zones    = $this->validateQueryStringArguments($_GET['zones'], 'zones'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $factions = $this->validateQueryStringArguments($_GET['factions'], 'factions'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $brackets = $this->validateQueryStringArguments($_GET['brackets'], 'brackets'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $dates    = $this->validateQueryStringArguments($_GET['dates'], 'dates'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } catch (InvalidArgumentException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             return $this->respondWithError($e->getMessage(), self::CODE_WRONG_ARGS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $offset = (int) $_GET['offset']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $limit  = (int) $_GET['limit']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         // Set defaults if not supplied | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if (empty($offset) || ! is_numeric($offset)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $offset = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if (empty($limit) || ! is_numeric($limit)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $limit = 50; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         // Check the date difference between two dates. we don't want to run queries for ALL OF ZE ALERTS NOW do we?! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         if (! empty($dates)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $this->getDateValidationUtility()->validateTimeDifference($dates, 180); // Allow half a year | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             } catch (InvalidArgumentException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 return $this->respondWithError($e->getMessage(), self::CODE_WRONG_ARGS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $limit = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $query = $this->repository->newQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $brackets = $this->convertStringToArrayForAuraBinds($brackets); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $brackets[] = 'UNK'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $query->cols(['*']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $query->where('ResultServer IN (?)', $this->convertStringToArrayForAuraBinds($servers)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $query->where('ResultAlertCont IN (?)', $this->convertStringToArrayForAuraBinds($zones)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $query->where('ResultWinner IN (?)', $this->convertStringToArrayForAuraBinds($factions)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $query->where('ResultTimeType IN (?)', $brackets); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if (! empty($dates)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $this->addDateRangeWhereClause($dates, $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $query->orderBy(["ResultEndTime DESC"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         if ($limit) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $query->limit($limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $query->offset($offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $history = $this->repository->fireStatementAndReturn($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return $this->respond( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             'collection', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $history, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $this->transformer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |  | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.