| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @package: chapi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author: bthapaliya | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @since: 2016-12-14 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Chapi\BusinessCase\Comparison; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Chapi\Component\Comparison\DiffCompareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Chapi\Entity\Chronos\ChronosJobEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Chapi\Entity\Chronos\JobCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Chapi\Entity\JobEntityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Chapi\Entity\Marathon\AppEntity\PortDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Chapi\Entity\Marathon\MarathonAppEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Chapi\Service\JobRepository\JobRepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class MarathonJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param JobRepositoryInterface $oLocalRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param JobRepositoryInterface $oRemoteRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param DiffCompareInterface $oDiffCompare | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 19 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         JobRepositoryInterface $oLocalRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         JobRepositoryInterface $oRemoteRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         DiffCompareInterface $oDiffCompare | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 19 |  |         $this->oRemoteRepository = $oRemoteRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 19 |  |         $this->oLocalRepository = $oLocalRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 19 |  |         $this->oDiffCompare = $oDiffCompare; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 19 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 1 |  |     protected function preCompareModifications(JobEntityInterface &$oLocalJob, JobEntityInterface &$oRemoteJob) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         if ( | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |             !$oLocalJob instanceof MarathonAppEntity || | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |             !$oRemoteJob instanceof MarathonAppEntity | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         ) | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         // marathon returns portDefinitions values for auto configured port as well | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         // we want to only check if the port is defined in local file. | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         // otherwise we ignore the remote values. | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 1 |  |         if (!$oLocalJob->portDefinitions) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |             $oRemoteJob->portDefinitions = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return JobEntityInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     protected function getEntitySetWithDefaults() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return new MarathonAppEntity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param JobEntityInterface|ChronosJobEntity $oJobEntityA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param JobEntityInterface|ChronosJobEntity $oJobEntityB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function hasSameJobType(JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         // for now we don't have a concrete seperation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // of types for marathon. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param $sProperty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param $oJobEntityA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param $oJobEntityB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 16 |  |     protected function isEntityEqual($sProperty, JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 16 |  |             !$oJobEntityA instanceof MarathonAppEntity || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 16 |  |             !$oJobEntityB instanceof MarathonAppEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 16 |  |         return $this->isEqual($oJobEntityA->{$sProperty}, $oJobEntityB->{$sProperty}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param mixed $mValueA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param mixed $mValueB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 16 |  |     private function isEqual($mValueA, $mValueB) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 16 |  |         if (is_array($mValueA) && is_array($mValueB)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 7 |  |             return $this->isArrayEqual($mValueA, $mValueB); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 10 |  |         elseif (is_object($mValueA) && is_object($mValueB)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 3 |  |             return $this->isArrayEqual(get_object_vars($mValueA), get_object_vars($mValueB)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 9 |  |         elseif ((is_scalar($mValueA) && is_scalar($mValueB)) || (is_null($mValueA) && is_null($mValueB))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 6 |  |             return $mValueA == $mValueB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 4 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param array $aValuesA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param array $aValuesB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 10 |  |     private function isArrayEqual(array $aValuesA, array $aValuesB) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 10 |  |         return $this->isArrayHalfEqual($aValuesA, $aValuesB) && $this->isArrayHalfEqual($aValuesB, $aValuesA); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param array $aValuesA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param array $aValuesB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 10 |  |     private function isArrayHalfEqual(array $aValuesA, array $aValuesB) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 10 |  |         foreach ($aValuesA as $_mKeyA => $_mValueA) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 9 |  |             if (is_string($_mKeyA)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 3 |  |                 if (!array_key_exists($_mKeyA, $aValuesB) || !$this->isEqual($_mValueA, $aValuesB[$_mKeyA])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 3 |  |                     return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 6 |  |                 foreach ($aValuesB as $_mValueB) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 6 |  |                     if ($_mValueA == $_mValueB) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 6 |  |                         continue 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 5 |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 7 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 162 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |  | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.