| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace hipanel\modules\finance\forms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use hipanel\helpers\ArrayHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use hipanel\modules\finance\models\ServerResource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use yii\web\UnprocessableEntityHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class VdsTariffForm extends AbstractTariffForm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public $note; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public $label; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function load($data, $formName = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->setAttributes($data[$this->formName()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $this->setResources($data[(new ServerResource())->formName()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function rules() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $rules = parent::rules(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $rules[] = [['note', 'label'], 'safe', 'on' => ['create', 'update']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         return $rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return \hipanel\modules\finance\models\ServerResource[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 33 |  | View Code Duplication |     public function getHardwareResources() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         /** @var ServerResource[] $resources */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $resources = array_filter($this->tariff->resources, function ($model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             /** @var ServerResource $model */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             return $model->isHardwareTypeCorrect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $order = array_keys(reset($resources)->getHardwareTypes()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return $this->sortResourcesByDefinedOrder($resources, $order, 'model_type'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param ServerResource[] $resources | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param array $order array of ordered values. $resources array will be re-ordered according this order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param string $key the key that will be used to re-order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private function sortResourcesByDefinedOrder($resources, $order, $key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $resources = ArrayHelper::index($resources, $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         foreach ($order as $type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             if (isset($resources[$type])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 $result[] = $resources[$type]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return \hipanel\modules\finance\models\ServerResource[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 68 |  | View Code Duplication |     public function getOveruseResources() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         /** @var ServerResource[] $resources */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $resources = array_filter($this->tariff->resources, function ($model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             /** @var ServerResource $model */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             return $model->isTypeCorrect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $order = array_keys(reset($resources)->getTypes()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return $this->sortResourcesByDefinedOrder($resources, $order, 'type'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function getParentOveruseResource($type_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return reset(array_filter($this->parentTariff->resources, function ($resource) use ($type_id) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             /** @var ServerResource $resource */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return $resource->type_id == $type_id && $resource->isTypeCorrect(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return \hipanel\modules\finance\models\ServerResource[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function getParentHardwareResource($object_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return reset(array_filter($this->parentTariff->resources, function ($resource) use ($object_id) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             /** @var ServerResource $resource */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             return $resource->object_id == $object_id && $resource->isHardwareTypeCorrect(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |     /** @inheritdoc */ | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 101 |  | View Code Duplication |     public function setResources($resources) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         $result = []; | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         foreach ((array) $resources as $resource) { | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |             if ($resource instanceof ServerResource) { | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |                 $result[] = $resource; | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |                 continue; | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |             $model = new ServerResource(['scenario' => $this->scenario]); | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |             if ($model->load($resource, '') && $model->validate()) { | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |                 $result[] = $model; | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |             } else { | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |                 throw new UnprocessableEntityHttpException('Failed to load resource model: ' . reset($model->getFirstErrors())); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |         $this->_resources = $result; | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |         return $this; | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.