ObjectAggregate::removeObject()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 9
cts 9
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 4
1
<?php
2
3
namespace SimpleAcl\Object;
4
5
use SimpleAcl\BaseObject;
6
7
/**
8
 * Implement common function for Role and Resources.
9
 *
10
 * @package SimpleAcl\Object
11
 */
12
abstract class ObjectAggregate
13
{
14
  /**
15
   * @var BaseObject[]
16
   */
17
  protected $objects = [];
18 2
19
  protected function removeObjects()
20 2
  {
21 2
    $this->objects = [];
22
  }
23
24
  /**
25
   * @param BaseObject|string $objectName
26
   *
27
   * @return bool
28 3
   */
29 View Code Duplication
  protected function removeObject($objectName)
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...
30 3
  {
31 2
    if ($objectName instanceof BaseObject) {
32 2
      $objectName = $objectName->getName();
33
    }
34 3
35 3
    foreach ($this->objects as $objectIndex => $object) {
36 3
      if ($object->getName() === $objectName) {
37
        unset($this->objects[$objectIndex]);
38 3
39
        return true;
40 3
      }
41
    }
42 2
43
    return false;
44
  }
45
46
  /**
47
   * @return array|BaseObject[]
48 12
   */
49
  protected function getObjects()
50 12
  {
51
    return $this->objects;
52
  }
53
54
  /**
55
   * @param array $objects
56 2
   */
57
  protected function setObjects($objects)
58
  {
59 2
    /** @var \SimpleAcl\BaseObject $object */
60 2
    foreach ($objects as $object) {
61 2
      $this->addObject($object);
62 2
    }
63
  }
64
65
  /**
66
   * @param \SimpleAcl\BaseObject $object
67 17
   */
68
  protected function addObject(BaseObject $object)
69 17
  {
70 2
    if ($this->getObject($object)) {
71
      return;
72
    }
73 17
74 17
    $this->objects[] = $object;
75
  }
76
77
  /**
78
   * @param BaseObject|string $objectName
79
   *
80
   * @return null|BaseObject
81 17
   */
82 View Code Duplication
  protected function getObject($objectName)
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...
83 17
  {
84 17
    if ($objectName instanceof BaseObject) {
85 17
      $objectName = $objectName->getName();
86
    }
87 17
88 16
    foreach ($this->objects as $object) {
89 4
      if ($object->getName() === $objectName) {
90
        return $object;
91 17
      }
92
    }
93 17
94
    return null;
95
  }
96
97
  /**
98
   * @return array
99 8
   */
100
  protected function getObjectNames(): array
101 8
  {
102
    $names = [];
103 8
104 7
    foreach ($this->objects as $object) {
105 8
      $names[] = $object->getName();
106
    }
107 8
108
    return $names;
109
  }
110
}
111