GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 2ec89f...0b541d )
by Alexey
13:03
created

BFrontendMenu::getDataProvider()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 0
dl 0
loc 19
ccs 0
cts 12
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Nikita Melnikov <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2014 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 * @package backend.modules.menu.models
8
 *
9
 * @property integer $id
10
 * @property BFrontendMenuItem[] $entries
11
 * @property BAbstractMenuEntry[] $availableEntries
12
 * @property string $name
13
 * @property string $sysname
14
 * @property string $url
15
 * @property integer $visible
16
 *
17
 * @method static BFrontendMenu model(string $class = __CLASS__)
18
 */
19
class BFrontendMenu extends BAbstractMenuEntry
20
{
21
  /**
22
   * Массив с доступными для меню классами
23
   * 'class' - название модели
24
   * 'key'   - ключ, определяющий участие записи в меню
25
   *
26
   * @var array
27
   */
28
  protected $availableClasses = array(
29
    array('class' => 'BFrontendMenu', 'key'   => 'visible'),
30
    array('class' => 'BInfo', 'key' => 'menu'),
31
    array('class' => 'BFrontendCustomMenuItem', 'key' => 'visible'),
32
  );
33
34
  /**
35
   * @var BFrontendCustomMenuItem[]
36
   */
37
  protected $availableEntries = array();
38
39
  /**
40
   * Загрузка дополнительных классов для работы с меню
41
   * @HINT: По большей части нужны классы, доступные как элементы меню
42
   */
43 5
  public static function loadExtraModels()
44
  {
45 5
    Yii::import('backend.modules.info.models.*');
46 5
  }
47
48
  /**
49
   * @param string $scenario
50
   */
51 5
  public function __construct($scenario = 'insert')
52
  {
53 5
    self::loadExtraModels();
54 5
    return parent::__construct($scenario);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
55
  }
56
57
  /**
58
   * @return int
59
   */
60
  public function getId()
61
  {
62
    return $this->id;
63
  }
64
65
  /**
66
   * @return string
67
   */
68
  public function getName()
69
  {
70
    return $this->name;
71
  }
72
73
  /**
74
   * @return string
75
   */
76
  public function getUrl()
77
  {
78
    return $this->url;
79
  }
80
81
  /**
82
   * @return string
83
   */
84
  public function getFrontendModelName()
85
  {
86
    return 'Menu';
87
  }
88
89
  /**
90
   * @return string
91
   */
92 6
  public function tableName()
93
  {
94 6
    return '{{menu}}';
95
  }
96
97
  /**
98
   * @return array
99
   */
100 4 View Code Duplication
  public function rules()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
101
  {
102
    return array(
103 4
      array('name, sysname', 'required'),
104 4
      array('name, sysname', 'length', 'max' => 255),
105 4
      array('name, sysname, url', 'safe'),
106
107 4
    );
108
  }
109
110
  /**
111
   * @return array
112
   */
113 1
  public function relations()
114
  {
115
    return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('entries' =...MenuItem', 'menu_id')); (array<string,string[]>) is incompatible with the return type of the parent method BActiveRecord::relations of type array<string,array<*,string>>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
116 1
      'entries' => array(self::HAS_MANY, 'BFrontendMenuItem', 'menu_id'),
117 1
    );
118
  }
119
120
  /**
121
   * Добавление записи меню
122
   *
123
   * @param IBFrontendMenuEntry $e
124
   *
125
   * @return bool
126
   */
127 2
  public function addEntry(IBFrontendMenuEntry $e)
128
  {
129 2
    if( !empty($this->id) && $e->getId() !== null )
130 2
    {
131 2
      $entry                 = new BFrontendMenuItem();
132 2
      $entry->item_id        = $e->getId();
133 2
      $entry->menu_id        = $this->id;
134 2
      $entry->type           = get_class($e);
135 2
      $entry->frontend_model = $e->getFrontendModelName();
136
137 2
      return $entry->save();
138
    }
139
140 1
    return false;
141
  }
142
143
  /**
144
   * Удаление записи меню
145
   *
146
   * @param IBFrontendMenuEntry $e
147
   *
148
   * @return bool
149
   */
150
  public function removeEntry(IBFrontendMenuEntry $e)
151
  {
152
    if( !empty($this->id) )
153
    {
154
      $criteria = new CDbCriteria();
155
      $criteria->compare('menu_id', $this->id);
156
      $criteria->compare('item_id', $e->getId());
157
      $criteria->compare('type', get_class($e));
158
159
      return BFrontendMenuItem::model()->find($criteria)->delete();
160
    }
161
162
    return false;
163
  }
164
165
  /**
166
   * Переключение отношения записи к меню
167
   *
168
   * @param IBFrontendMenuEntry $e
169
   */
170
  public function switchMenuEntryStatus(IBFrontendMenuEntry $e)
171
  {
172
    if( $this->hasMenuEntry($e) )
173
      $this->removeEntry($e);
174
    else
175
      $this->addEntry($e);
176
  }
177
178
  /**
179
   * Является ли запись CustomMenuItem элементом меню
180
   *
181
   * @param BFrontendCustomMenuItem $c
182
   *
183
   * @return bool
184
   */
185 1
  public function hasCustomMenuItem(BFrontendCustomMenuItem $c)
186
  {
187 1
    return $this->hasMenuEntry($c);
188
  }
189
190
  /**
191
   * Является ли $e записью меню
192
   *
193
   * @param IBFrontendMenuEntry $e
194
   *
195
   * @return bool
196
   */
197 1
  public function hasMenuEntry(IBFrontendMenuEntry $e)
198
  {
199 1
    $criteria = new CDbCriteria();
200 1
    $criteria->compare('type', get_class($e));
201 1
    $criteria->compare('item_id', $e->getId());
202 1
    $criteria->compare('menu_id', $this->id);
203
204 1
    return (boolean) BFrontendMenuItem::model()->find($criteria);
205
  }
206
207
  /**
208
   * @return CArrayDataProvider
209
   */
210
  public function getDataProvider()
211
  {
212
    $data = array();
213
214
    foreach( $this->entries as $entry )
215
    {
216
      $item = BFrontendMenuGridAdapter::convertFromMenuEntry($entry);
217
      $data[] = new BFrontendMenuGridAdapter($item, true, $entry->position);
218
    }
219
220
    foreach( $this->getAvailableEntries() as $entry )
221
    {
222
      $data[] = new BFrontendMenuGridAdapter($entry, false);
223
    }
224
225
    $dataProvider = new CArrayDataProvider($data);
226
    $dataProvider->getPagination()->pageSize = 30;
227
    return $dataProvider;
228
  }
229
230
  /**
231
   * @return BAbstractMenuEntry[]
232
   * @throws BFrontendMenuException
233
   */
234
  public function getAvailableEntries()
235
  {
236
    if( empty($this->availableEntries) )
237
    {
238
      foreach( $this->availableClasses as $class )
239
      {
240
        $className = $class['class'];
241
242
        if( new $className() instanceof IBFrontendMenuEntry )
243
        {
244
          $this->availableEntries = CMap::mergeArray(
245
              $className::model()->findAll($class['key'] . ' = 1'),
246
              $this->availableEntries
247
          );
248
        }
249
        else
250
          throw new BFrontendMenuException('Невозможно использовать класс для добавления к меню', BFrontendMenuException::WRONG_CLASS_INHERITANCE);
251
      }
252
253
      $this->clearEntries();
254
    }
255
256
    return $this->availableEntries;
257
  }
258
259
  /**
260
   * @param CDbCriteria $criteria
261
   *
262
   * @return CDbCriteria
263
   */
264 View Code Duplication
  protected function getSearchCriteria(CDbCriteria $criteria)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
265
  {
266
    $criteria->compare('name', $this->name, true);
267
    $criteria->compare('sysname', $this->sysname, true);
268
    $criteria->compare('url', $this->url, true);
269
270
    return $criteria;
271
  }
272
273
  /**
274
   * Удаление из доступных элементов для добавления уже добавленных
275
   */
276
  protected function clearEntries()
277
  {
278
    foreach( $this->availableEntries as $key => $item )
279
    {
280
      foreach( $this->entries as $entry )
281
      {
282
        if( $entry->getModel()->getId() === $item->getId() && $entry->getModelClass() === get_class($item) )
283
          unset($this->availableEntries[$key]);
284
      }
285
286
      if( $item->id == $this->id && get_class($item) == get_class($this) )
287
        unset($this->availableEntries[$key]);
288
    }
289
  }
290
}