| Total Complexity | 8 |
| Total Lines | 63 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class GateFilter implements FilterInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * The Laravel gate instance, used to check for permissions. |
||
| 11 | * |
||
| 12 | * @var Gate |
||
| 13 | */ |
||
| 14 | protected $gate; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Constructor. |
||
| 18 | * |
||
| 19 | * @param Gate $gate |
||
| 20 | */ |
||
| 21 | 44 | public function __construct(Gate $gate) |
|
| 22 | { |
||
| 23 | 44 | $this->gate = $gate; |
|
| 24 | 44 | } |
|
| 25 | |||
| 26 | /** |
||
| 27 | * Transforms a menu item. Add the restricted property to a menu item |
||
| 28 | * when situable. |
||
| 29 | * |
||
| 30 | * @param array $item A menu item |
||
| 31 | * @return array The transformed menu item |
||
| 32 | */ |
||
| 33 | 44 | public function transform($item) |
|
| 34 | { |
||
| 35 | // Set a special attribute when item is not allowed. Items with this |
||
| 36 | // attribute will be filtered out of the menu. |
||
| 37 | |||
| 38 | 44 | if (! $this->isAllowed($item)) { |
|
| 39 | 4 | $item['restricted'] = true; |
|
| 40 | } |
||
| 41 | |||
| 42 | 44 | return $item; |
|
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Check if a menu item is allowed for the current user. |
||
| 47 | * |
||
| 48 | * @param array $item A menu item |
||
| 49 | * @return bool |
||
| 50 | */ |
||
| 51 | 44 | protected function isAllowed($item) |
|
| 70 | } |
||
| 71 | } |
||
| 72 |