PartitionContainer::addItemToPartition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace drupol\phpartition;
4
5
/**
6
 * Class PartitionContainer.
7
 *
8
 * @package drupol\phpartition
9
 */
10
class PartitionContainer extends \SplHeap
11
{
12
13
  /**
14
   * The algorithm to use.
15
   *
16
   * @var BasePartitionAlgorithm
17
   */
18
    protected $algo;
19
20
  /**
21
   * The number of partition to use.
22
   *
23
   * @var int
24
   */
25
    protected $size;
26
27
  /**
28
   * Override compare method.
29
   *
30
   * @param Partition $partitionA
31
   *   The first partition.
32
   * @param Partition $partitionB
33
   *   The second partition.
34
   *
35
   *   {@inheritdoc}.
36
   */
37 14
    public function compare($partitionA, $partitionB)
38
    {
39 14
        $partitionAWeight = $partitionA->getAlgo()->getPartitionWeight($partitionA);
40 14
        $partitionBWeight = $partitionB->getAlgo()->getPartitionWeight($partitionB);
41
42 14
        if ($partitionAWeight == $partitionBWeight) {
43 14
            return 0;
44
        }
45
46 14
        return ($partitionAWeight > $partitionBWeight) ? -1 : +1;
47
    }
48
49
  /**
50
   * Set the size of the container.
51
   *
52
   * @param int $size
53
   *   The size.
54
   */
55 14
    public function setSize($size)
56
    {
57 14
        $this->size = $size;
58
59 14
        for ($i = 0; $i < $size; $i++) {
60 14
            $subset = new Partition();
61 14
            $subset->setAlgo($this->getAlgo());
62 14
            $this->insert($subset);
63 14
        }
64 14
    }
65
66
  /**
67
   * Return the size of the container.
68
   *
69
   * @return int
70
   *   The number of partitions the container has.
71
   */
72 14
    public function getSize()
73
    {
74 14
        return $this->size;
75
    }
76
77
  /**
78
   * Add items to the partition.
79
   *
80
   * @param PartitionItem[] $items
81
   *   The items to add.
82
   */
83 6
    public function addItemsToPartition(array $items = array())
84
    {
85 6
        foreach ($items as $item) {
86 6
            $this->addItemToPartition($item);
87 6
        }
88 6
    }
89
90
  /**
91
   * Add an item to the first partition.
92
   *
93
   * @param \drupol\phpartition\PartitionItem $item
94
   *   The item to add.
95
   */
96 6
    public function addItemToPartition(PartitionItem $item)
97
    {
98 6
        $this->top();
99 6
        $subset = $this->extract();
100 6
        $subset->addItem($item);
101 6
        $this->insert($subset);
102 6
    }
103
104
  /**
105
   * Get the partition in the container.
106
   *
107
   * @return Partition[]
108
   *   An array of partitions.
109
   */
110 14 View Code Duplication
    public function getPartitions()
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...
111
    {
112 14
        $data = array();
113 14
        $clone = clone $this;
114
115 14
        for ($clone->top(); $clone->valid(); $clone->next()) {
116 14
            $data[] = $clone->current();
117 14
        }
118
119 14
        return $data;
120
    }
121
122
  /**
123
   * Return the items from each partitions in the container.
124
   *
125
   * @return mixed[]
126
   *   The items.
127
   */
128 14 View Code Duplication
    public function getPartitionsItemsArray()
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...
129
    {
130 14
        $data = array();
131
132 14
        foreach ($this->getPartitions() as $subset) {
133 14
            $data[] = $subset->getRawItems();
134 14
        }
135
136 14
        return array_values(array_filter($data));
137
    }
138
139
  /**
140
   * Set the algorithm to use.
141
   *
142
   * @param \drupol\phpartition\BasePartitionAlgorithm $algo
143
   *   The algorithm.
144
   */
145 14
    public function setAlgo(BasePartitionAlgorithm $algo)
146
    {
147 14
        $this->algo = $algo;
148 14
    }
149
150
  /**
151
   * Get the algorithm.
152
   *
153
   * @return BasePartitionAlgorithm
154
   *   The algorithm.
155
   */
156 14
    public function getAlgo()
157
    {
158 14
        return $this->algo;
159
    }
160
}
161