Passed
Push — main ( 2aa0f9...2be1e4 )
by Sammy
07:47 queued 01:51
created

HasOperator::operator()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 12
nc 12
nop 1
dl 0
loc 23
rs 8.4444
c 1
b 0
f 0
1
<?php
2
3
namespace HexMakina\kadro\Auth;
4
5
use HexMakina\BlackBox\Auth\OperatorInterface;
6
use HexMakina\BlackBox\ORM\ModelInterface;
7
8
trait HasOperator
9
{
10
  use \HexMakina\TightORM\HasOne;
0 ignored issues
show
Bug introduced by
The type HexMakina\TightORM\HasOne was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
  private $operator = null;
13
14
  abstract public function get($prop_name);
15
16
  public function operator(OperatorInterface $setter=null)
17
  {
18
    if(!is_null($setter))
19
      $this->operator = $setter;
20
21
    if(is_null($this->operator))
22
    {
23
      $extract_attempt = self::extract(new Operator(), $this, true);
24
      if (!is_null($extract_attempt)) {
25
          foreach (['permission_names', 'permission_ids'] as $permission_marker) {
26
              if (property_exists($this, $permission_marker)) {
27
                  $extract_attempt->set($permission_marker, $this->$permission_marker);
28
              }
29
          }
30
31
          $this->operator = $extract_attempt;
32
      }
33
    }
34
35
    if(is_null($this->operator) && !empty($this->get('operator_id')))
36
      $this->operator = Operator::exists($this->get('operator_id'));
37
38
    return $this->operator;
39
  }
40
41
42
  public static function enhance_query_retrieve($Query, $filters, $options)
43
  {
44
      $Query->autoJoin([ACL::table(),'ACL'], null, 'LEFT OUTER');
45
      $permission_alias = $Query->autoJoin([Permission::table(), 'permission'], null, 'LEFT OUTER');
46
47
      $permission_ids_and_names = [];
48
      $permission_ids_and_names [] = sprintf('GROUP_CONCAT(DISTINCT %s.%s) as %s', $permission_alias, 'id', $permission_alias . '_ids');
49
      $permission_ids_and_names [] = sprintf('GROUP_CONCAT(DISTINCT %s.%s) as %s', $permission_alias, 'name', $permission_alias . '_names');
50
      $Query->selectAlso($permission_ids_and_names);
51
52
      $Query->selectAlso(['operator.name as operator_name', 'operator.active as operator_active']);
53
54
      if (isset($filters['username'])) {
55
          $Query->whereEQ('username', $filters['username'], 'operator');
56
      }
57
58
      if (isset($filters['email'])) {
59
          $Query->whereEQ('email', $filters['email'], 'operator');
60
      }
61
62
      if (isset($filters['active'])) {
63
          $Query->whereEQ('active', $filters['active'], 'operator');
64
      }
65
66
      return $Query;
67
  }
68
}
69