Completed
Push — master ( b0612a...e62e65 )
by Pol
06:37
created

Partition::deleteItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace drupol\phpartition;
4
5
use PhpCollection\Map;
6
use PhpCollection\MapInterface;
7
8
/**
9
 * Class Partition.
10
 *
11
 * @package drupol\phpartition
12
 */
13
class Partition extends Map implements MapInterface {
14
15
  /**
16
   * The algorithm in use.
17
   *
18
   * @var PartitionAlgorithmInterface
19
   */
20
  protected $algo;
21
22
  /**
23
   * Add an item to the partition.
24
   *
25
   * @param PartitionItem $item
26
   *   The item to add to the partition.
27
   */
28 10
  public function addItem(PartitionItem $item) {
29 10
    $this->set(spl_object_hash($item), $item);
30 10
  }
31
32
  /**
33
   * Add items to the partition.
34
   *
35
   * @param PartitionItem[] $items
36
   *   The items to add to the partition.
37
   */
38 10
  public function addItems(array $items = array()) {
39 10
    foreach ($items as $item) {
40 10
      $this->addItem($item);
41
    }
42 10
  }
43
44
  /**
45
   * Get an array of original items.
46
   *
47
   * @return array
48
   *   The original items.
49
   */
50 10
  public function getRawItems() {
51 10
    return array_values(
52
      array_map(function ($item) {
53 10
        return $item->getItem();
54 10
      }, $this->all())
55
    );
56
  }
57
58
  /**
59
   * Get the weight of the partition.
60
   *
61
   * @return int
62
   *   The partition's weight.
63
   */
64 8
  public function getWeight() {
65
    return array_reduce($this->all(), function ($sum, $item) {
66 8
      $sum += $item->getValue();
67 8
      return $sum;
68 8
    });
69
  }
70
71
  /**
72
   * Set the algorithm to use.
73
   *
74
   * @param BasePartitionAlgorithm $algo
75
   *   The algorithm to use.
76
   */
77 10
  public function setAlgo(BasePartitionAlgorithm $algo) {
78 10
    $this->algo = $algo;
79 10
  }
80
81
  /**
82
   * Get the algorithm in use.
83
   *
84
   * @return PartitionAlgorithmInterface
85
   *   The algorithm in use.
86
   */
87 10
  public function getAlgo() {
88 10
    return $this->algo;
89
  }
90
91
  /**
92
   * Delete items from the partition.
93
   *
94
   * @param PartitionItem[] $items
95
   *   The items to delete.
96
   */
97 2
  public function deleteItems(array $items = array()) {
98 2
    foreach ($items as $item) {
99 2
      $this->delete($item);
100
    }
101 2
  }
102
103
  /**
104
   * Delete an item from the partition.
105
   *
106
   * @param PartitionItem $item
107
   *   The item to delete.
108
   */
109 2
  public function delete(PartitionItem $item) {
110 2
    $this->remove(spl_object_hash($item));
111 2
  }
112
113
  /**
114
   * Sort the items of the partition in a particular order.
115
   *
116
   * @param string $order
117
   *   ASC for ascending, DESC for descending.
118
   *
119
   * @return $this
120
   */
121 8
  public function sortByValue($order = 'ASC') {
122 8 View Code Duplication
    if ('ASC' == $order) {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
123
      $this->sortWith(function ($itemA, $itemB) {
124 6
        return $this->get($itemA)->get()->getValue() > $this->get($itemB)->get()->getValue();
125 6
      });
126
    }
127
128 8 View Code Duplication
    if ('DESC' == $order) {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
129 2
      $this->sortWith(function ($itemA, $itemB) {
130 2
        return $this->get($itemA)->get()->getValue() < $this->get($itemB)->get()->getValue();
131 2
      });
132
    }
133
134 8
    return $this;
135
  }
136
137
}
138