ServicesOrdering::canUseService()   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
namespace kalanis\EmailApi\LocalInfo;
4
5
6
use Iterator;
7
use kalanis\EmailApi\Interfaces;
8
9
10
/**
11
 * Class ServicesOrdering
12
 * @package kalanis\EmailApi\LocalInfo
13
 * Default services available on local installation
14
 * Contains typical PHP object-to-array boilerplate
15
 * You can extend this class to specify order of available services
16
 * @implements Iterator<int|null, Interfaces\ISending|null>
17
 */
18
class ServicesOrdering implements Iterator
19
{
20
    /** @var array<int, Interfaces\ISending> */
21
    protected array $services = [];
22
    protected bool $returnOnUnsuccessful = false;
23
24 9
    public function addService(Interfaces\ISending $service): self
25
    {
26 9
        $this->services[$service->systemServiceId()] = $service;
27 9
        return $this;
28
    }
29
30 1
    public function removeService(Interfaces\ISending $service): self
31
    {
32 1
        unset($this->services[$service->systemServiceId()]);
33 1
        return $this;
34
    }
35
36 1
    public function mayReturnFirstUnsuccessful(bool $set = false): self
37
    {
38 1
        $this->returnOnUnsuccessful = $set;
39 1
        return $this;
40
    }
41
42 1
    public function isReturningAfterFirstUnsuccessful(): bool
43
    {
44 1
        return $this->returnOnUnsuccessful;
45
    }
46
47 10
    public function canUseService(): bool
48
    {
49 10
        return (!empty($this->services));
50
    }
51
52
    #[\ReturnTypeWillChange]
53 7
    public function current()
54
    {
55 7
        return (false !== ($service = current($this->services))) ? $service : null;
56
    }
57
58 1
    public function next(): void
59
    {
60 1
        next($this->services);
61 1
    }
62
63
    #[\ReturnTypeWillChange]
64 7
    public function key()
65
    {
66 7
        return key($this->services);
67
    }
68
69 7
    public function valid(): bool
70
    {
71 7
        $key = $this->key();
72 7
        return !is_null($key);
73
    }
74
75 7
    public function rewind(): void
76
    {
77 7
        reset($this->services);
78 7
    }
79
}
80