Passed
Push — master ( e95921...ddbd1b )
by Dimas
13:28 queued 05:15
created

access::get_managed_access()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 24
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 43
rs 9.2248
1
<?php
2
3
namespace User;
4
5
class access
6
{
7
  private $config;
8
  /**
9
   * User instance.
10
   *
11
   * @var \User\user
12
   */
13
  private $user;
14
15
  public function __construct(\User\user $user)
16
  {
17
    $this->config = \Filemanager\file::file(__DIR__ . '/config/access/global.json', ['superuser' => '*']);
0 ignored issues
show
Bug introduced by
array('superuser' => '*') of type array<string,string> is incompatible with the type boolean expected by parameter $create of Filemanager\file::file(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

17
    $this->config = \Filemanager\file::file(__DIR__ . '/config/access/global.json', /** @scrutinizer ignore-type */ ['superuser' => '*']);
Loading history...
18
    $this->user = $user;
19
  }
20
21
  function check()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
  {
23
  }
24
25
  /**
26
   * Get access lists.
27
   *
28
   * @return array
29
   */
30
  public function getAccess()
31
  {
32
    return \Filemanager\file::get($this->config, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Filemanager\file::get($this->config, true) also could return the type string which is incompatible with the documented return type array.
Loading history...
33
  }
34
35
  /**
36
   * Get root config.json.
37
   *
38
   * @return array
39
   */
40
  private function config()
41
  {
42
    return \Filemanager\file::get(ROOT . '/config.json', true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Filemanager\file:.... '/config.json', true) also could return the type string which is incompatible with the documented return type array.
Loading history...
43
  }
44
45
  private $managedAccess;
46
47
  /**
48
   * Get list of managed access, that requires spesific user roles.
49
   *
50
   * @return array
51
   */
52
  public function get_managed_access()
53
  {
54
    if (!$this->managedAccess) {
55
      $app = $this->config();
56
      if (isset($app['app'])) {
57
        $app = $app['app'];
58
      } else {
59
        throw new \MVC\Exception('Config failed, no `app` property', 1);
60
      }
61
      $read = \Filemanager\scan::scandir(ROOT . '/' . $app['views']);
62
      $flist = [];
63
      foreach ($read as $files) {
64
        $flist[] = \MVC\helper::get_url_path($files['path']);
65
      }
66
      $flist = array_map(function ($path) use ($app) {
67
        //strip extensions
68
        $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $path);
69
        /**
70
         * Group ignore patterns.
71
         */
72
        $patterns = [
73
          // ignore static file and form controller
74
          "\-f.php$|\.(js|css|json|txt|log|htaccess|gitignore)$",
75
          // ignore not special access
76
          "\/(login|logout|signup|test)\.php$",
77
          // ignore unmanaged zones
78
          "\/(fb|css|chat|comments|curl|snippets|vpngate|blog|server|php|proxy|im3|telkomsel|byu|axis|google|auth|agc|alquran|img|tools|coupon|login|test)\/",
79
          // ignore public access
80
          "^\/{$app['views']}\/(index|dashboard|profile|php-socket|user)\.php$",
81
        ];
82
83
        $ignore = preg_match_all('/' . implode('|', $patterns) . '/s', $path);
84
        if (!$ignore) {
85
          return $withoutExt;
86
        }
87
88
        return null;
89
      }, $flist);
90
91
      $this->managedAccess = array_values(array_filter($flist));
92
    }
93
94
    return $this->managedAccess;
95
  }
96
}
97