Completed
Push — master ( b5d022...8c8f5f )
by Pol
02:28
created

Partition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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
   * Partition constructor.
24
   *
25
   * @param PartitionItem[] $elements
26
   *   The list of elements.
27
   */
28 14
  public function __construct(array $elements = array()) {
29 14
    parent::__construct(array());
30 14
    $this->addItems($elements);
31 14
  }
32
33
  /**
34
   * Add an item to the partition.
35
   *
36
   * @param PartitionItem $item
37
   *   The item to add to the partition.
38
   *
39
   * @return $this
40
   *   Return itself.
41
   */
42 14
  public function addItem(PartitionItem $item) {
43 14
    $this->set(spl_object_hash($item), $item);
44
45 14
    return $this;
46
  }
47
48
  /**
49
   * Add items to the partition.
50
   *
51
   * @param PartitionItem[] $items
52
   *   The items to add to the partition.
53
   *
54
   * @return $this
55
   *   Return itself.
56
   */
57 14
  public function addItems(array $items = array()) {
58 14
    foreach ($items as $item) {
59 14
      $this->addItem($item);
60
    }
61
62 14
    return $this;
63
  }
64
65
  /**
66
   * Get an array of original items.
67
   *
68
   * @return array
69
   *   The original items.
70
   */
71 14
  public function getRawItems() {
72 14
    return array_values(
73
      array_map(function ($item) {
74 14
        return $item->getItem();
75 14
      }, $this->all())
76
    );
77
  }
78
79
  /**
80
   * Get the weight of the partition.
81
   *
82
   * @return int
83
   *   The partition's weight.
84
   */
85 11
  public function getWeight() {
86
    return array_reduce($this->all(), function ($sum, $item) {
87 11
      $sum += $item->getValue();
88 11
      return $sum;
89 11
    });
90
  }
91
92
  /**
93
   * Set the algorithm to use.
94
   *
95
   * @param BasePartitionAlgorithm $algo
96
   *   The algorithm to use.
97
   */
98 14
  public function setAlgo(BasePartitionAlgorithm $algo) {
99 14
    $this->algo = $algo;
100 14
  }
101
102
  /**
103
   * Get the algorithm in use.
104
   *
105
   * @return PartitionAlgorithmInterface
106
   *   The algorithm in use.
107
   */
108 14
  public function getAlgo() {
109 14
    return $this->algo;
110
  }
111
112
  /**
113
   * Delete items from the partition.
114
   *
115
   * @param PartitionItem[] $items
116
   *   The items to delete.
117
   */
118 3
  public function deleteItems(array $items = array()) {
119 3
    foreach ($items as $item) {
120 3
      $this->delete($item);
121
    }
122 3
  }
123
124
  /**
125
   * Delete an item from the partition.
126
   *
127
   * @param PartitionItem $item
128
   *   The item to delete.
129
   */
130 3
  public function delete(PartitionItem $item) {
131 3
    $this->remove(spl_object_hash($item));
132 3
  }
133
134
  /**
135
   * Sort the items of the partition in a particular order.
136
   *
137
   * @param string $order
138
   *   ASC for ascending, DESC for descending.
139
   *
140
   * @return $this
141
   */
142 11
  public function sortByValue($order = 'ASC') {
143 11 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...
144
      $this->sortWith(function ($itemA, $itemB) {
145 8
        return $this->get($itemA)->get()->getValue() > $this->get($itemB)->get()->getValue();
146 8
      });
147
    }
148
149 11 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...
150 3
      $this->sortWith(function ($itemA, $itemB) {
151 3
        return $this->get($itemA)->get()->getValue() < $this->get($itemB)->get()->getValue();
152 3
      });
153
    }
154
155 11
    return $this;
156
  }
157
158
}
159