This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * @author Sergey Glagolev <[email protected]> |
||
4 | * @link https://github.com/shogodev/argilla/ |
||
5 | * @copyright Copyright © 2003-2014 Shogo |
||
6 | * @license http://argilla.ru/LICENSE |
||
7 | */ |
||
8 | class MenuBuilder |
||
9 | { |
||
10 | /** |
||
11 | * @var ProductAssignment |
||
12 | */ |
||
13 | private $assignment; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $cachePrefix = 'menuBuilder'; |
||
19 | |||
20 | /** |
||
21 | * @var integer |
||
22 | */ |
||
23 | private $cacheExpire = 300; |
||
24 | |||
25 | private $imagePath = '/f/product/'; |
||
26 | |||
27 | 7 | public function __construct() |
|
28 | { |
||
29 | 7 | $this->assignment = ProductAssignment::model(); |
|
30 | 7 | } |
|
31 | |||
32 | /** |
||
33 | * @param $sysname |
||
34 | * |
||
35 | * @return array |
||
36 | */ |
||
37 | 7 | public function getMenu($sysname) |
|
38 | { |
||
39 | 7 | $key = __METHOD__.$sysname; |
|
40 | 7 | if( $this->cacheExists($key) ) return $this->getCache($key); |
|
41 | 7 | $menu = Menu::model()->getMenu($sysname); |
|
42 | 7 | $this->setCache($key, $menu); |
|
43 | |||
44 | 7 | return $menu; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * @return array |
||
49 | */ |
||
50 | public function getSectionMenu() |
||
51 | { |
||
52 | $key = $this->getKey(__METHOD__, func_get_args()); |
||
53 | if( $this->cacheExists($key) ) return $this->getCache($key); |
||
54 | |||
55 | $menu = array(); |
||
56 | $assignments = $this->assignment->getAssignments(); |
||
57 | |||
58 | foreach($assignments as $item) |
||
59 | { |
||
60 | View Code Duplication | if( !empty($item['section_id']) && !isset($menu[$item['section_id']]) ) |
|
0 ignored issues
–
show
|
|||
61 | $menu[$item['section_id']] = $this->buildSectionItem($item); |
||
62 | } |
||
63 | |||
64 | $this->sortRecursive($menu); |
||
65 | $this->setCache($key, $menu); |
||
66 | |||
67 | return $menu; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | public function getSectionTypeMenu() |
||
74 | { |
||
75 | $key = $this->getKey(__METHOD__, func_get_args()); |
||
76 | if( $this->cacheExists($key) ) return $this->getCache($key); |
||
77 | |||
78 | $menu = array(); |
||
79 | $assignments = $this->assignment->getAssignments(); |
||
80 | |||
81 | foreach($assignments as $item) |
||
82 | { |
||
83 | View Code Duplication | if( !empty($item['section_id']) && !isset($menu[$item['section_id']]) ) |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
84 | { |
||
85 | $menu[$item['section_id']] = $this->buildSectionItem($item); |
||
86 | } |
||
87 | |||
88 | if( !empty($item['type_id']) && !isset($menu[$item['section_id']]['items'][$item['type_id']]) ) |
||
89 | { |
||
90 | $menu[$item['section_id']]['items'][$item['type_id']] = $this->buildTypeItem($item); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | $this->sortRecursive($menu); |
||
95 | $this->setCache($key, $menu); |
||
96 | |||
97 | return $menu; |
||
98 | } |
||
99 | |||
100 | private function buildSectionItem($item) |
||
101 | { |
||
102 | return array( |
||
103 | 'label' => $item['section_name'], |
||
104 | 'position' => $item['section_position'], |
||
105 | 'url' => array('product/section', 'section' => $item['section_url']), |
||
106 | 'items' => array(), |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | private function buildTypeItem($item) |
||
111 | { |
||
112 | return array( |
||
113 | 'label' => $item['type_name'], |
||
114 | 'position' => $item['type_position'], |
||
115 | 'url' => array('product/type', 'type' => $item['type_url']), |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | private function buildImage($image, $default = '/i/sp.gif') |
||
0 ignored issues
–
show
|
|||
120 | { |
||
121 | return !empty($image) ? $this->imagePath.$image : $default; |
||
122 | } |
||
123 | |||
124 | private function sortRecursive(&$menu) |
||
125 | { |
||
126 | foreach($menu as $key => $element) |
||
127 | { |
||
128 | if( !empty($menu[$key]['items']) ) |
||
129 | $this->sortRecursive($menu[$key]['items']); |
||
130 | } |
||
131 | |||
132 | uasort($menu, function($a, $b) { |
||
133 | if( isset($a['position']) ) |
||
134 | { |
||
135 | if( $a['position'] == 0 ) |
||
136 | $a['position'] = 999999; |
||
137 | |||
138 | if( $b['position'] == 0 ) |
||
139 | $b['position'] = 999999; |
||
140 | |||
141 | if( $a['position'] > $b['position'] ) |
||
142 | return 1; |
||
143 | else if( $a['position'] < $b['position'] ) |
||
144 | return -1; |
||
145 | else |
||
146 | return strcmp($a['label'], $b['label']); |
||
147 | } |
||
148 | |||
149 | return strcmp($a['label'], $b['label']); |
||
150 | }); |
||
151 | } |
||
152 | |||
153 | private function getKey($method, $args) |
||
154 | { |
||
155 | return $method.crc32(serialize($args)); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param string $key |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | 7 | private function cacheExists($key) |
|
164 | { |
||
165 | 7 | return Yii::app()->cache->offsetExists($this->cachePrefix.$key); |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param $key |
||
170 | * |
||
171 | * @return mixed |
||
172 | */ |
||
173 | private function getCache($key) |
||
174 | { |
||
175 | return Yii::app()->cache->offsetGet($this->cachePrefix.$key); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $key |
||
180 | * @param mixed $data |
||
181 | */ |
||
182 | 7 | private function setCache($key, $data) |
|
183 | { |
||
184 | 7 | Yii::app()->cache->set($this->cachePrefix.$key, $data, $this->cacheExpire); |
|
185 | } |
||
186 | } |
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.