ActivityDtoCollection::current()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of forecast.it.fill project.
7
 * (c) Patrick Jaja <[email protected]>
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace ForecastAutomation\Activity\Shared\Dto;
13
14
use ArrayAccess;
15
use Iterator;
16
17
class ActivityDtoCollection implements Iterator, ArrayAccess
18
{
19
    /**
20
     * @var \ForecastAutomation\Activity\Shared\Dto\ActivityDto[]
21
     */
22
    public array $activityDtos;
23
24
    private int $position = 0;
25
26 6
    public function __construct(
27
        ActivityDto ...$activityDtos
28
    ) {
29 6
        $this->activityDtos = $activityDtos;
30 6
    }
31
32 3
    public function current(): ActivityDto
33
    {
34 3
        return $this->activityDtos[$this->position];
35
    }
36
37 3
    public function next(): void
38
    {
39 3
        ++$this->position;
40 3
    }
41
42
    public function key(): int
43
    {
44
        return $this->position;
45
    }
46
47 3
    public function valid(): bool
48
    {
49 3
        return \array_key_exists($this->position, $this->activityDtos);
50
    }
51
52 3
    public function rewind(): void
53
    {
54 3
        $this->position = 0;
55 3
    }
56
57
    public function offsetExists($offset): bool
58
    {
59
        return \array_key_exists($offset, $this->activityDtos);
60
    }
61
62 3
    public function offsetGet($offset): mixed
63
    {
64 3
        return $this->activityDtos[$offset] ?? null;
65
    }
66
67
    public function offsetSet($offset, $value): void
68
    {
69
        if (null === $offset) {
70
            $this->activityDtos[] = $value;
71
        } else {
72
            $this->activityDtos[$offset] = $value;
73
        }
74
    }
75
76
    public function offsetUnset($offset): void
77
    {
78
        unset($this->activityDtos[$offset]);
79
    }
80
81 1
    public function merge(self $activityDtoCollection): self
82
    {
83 1
        foreach ($activityDtoCollection as $activityDto) {
84 1
            if (! $this->sumDurationIfExist($activityDto)) {
85 1
                $this->activityDtos[] = $activityDto;
86
            }
87
        }
88
89 1
        return $this;
90
    }
91
92 1
    public function mergeFreedom(self $activityDtoCollection): self
93
    {
94 1
        foreach ($activityDtoCollection as $activityDto) {
95 1
            $this->activityDtos[] = $activityDto;
96
        }
97
98
        return $this;
99
    }
100
101
    private function sumDurationIfExist(ActivityDto $activityDto): bool
102 1
    {
103
        $exist = false;
104
        foreach ($this->activityDtos as $storedActivityDto) {
105
            if (strtolower($storedActivityDto->needle) === (strtolower($activityDto->needle))) {
106
                $storedActivityDto->duration += $activityDto->duration;
107
                $exist = true;
108
            }
109
        }
110
111
        return $exist;
112
    }
113
}
114