Passed
Push — master ( cdc883...5b7063 )
by Derek Stephen
08:42 queued 07:12
created

CountryRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 22
c 4
b 0
f 1
dl 0
loc 55
ccs 15
cts 25
cp 0.6
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findCountryBy() 0 9 3
A createFromArray() 0 10 1
A findCountryByIsoCode() 0 7 2
A findAllCountries() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Del\Repository;
6
7
use Del\Exception\CountryException;
8
use Del\Entity\Country;
9
use Del\Factory\CountryList;
10
11
class CountryRepository
12
{
13
    /** @var Country[] $countries */
14
    private array $countries;
15
16 1
    public function __construct()
17
    {
18 1
        $list = new CountryList();
19 1
        $this->countries = $list->getCountries();
20
    }
21
22
    /**  @return Country[]  */
23
    public function findAllCountries(): array
24
    {
25
        $countries = [];
26
27
        foreach ($this->countries as $country) {
28
            $countries[] = $this->createFromArray($country);
29
        }
30
31
        return $countries;
32
    }
33
34
    /** @throws CountryException  */
35 3
    public function findCountryByIsoCode(string $id): Country
36
    {
37 3
        if (isset($this->countries[$id])) {
38 2
            return $this->createFromArray($this->countries[$id]);
39
        }
40
41 1
        throw new CountryException(CountryException::ERROR_NOT_FOUND);
42
    }
43
44
    /** @throws CountryException */
45
    public function findCountryBy(string $key, string $value): Country
46
    {
47
        foreach ($this->countries as $country) {
48
            if ($country[$key] === $value) {
49
                return $this->createFromArray($this->countries[$id]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
50
            }
51
        }
52
53
        throw new CountryException(CountryException::ERROR_NOT_FOUND);
54
    }
55
56 2
    public function createFromArray(array $data): Country
57
    {
58 2
        $country = new Country();
59 2
        $country->setId($data['id']);
60 2
        $country->setIso($data['iso']);
61 2
        $country->setName($data['name']);
62 2
        $country->setNumCode($data['numcode']);
63 2
        $country->setFlag($data['flag']);
64
65 2
        return $country;
66
    }
67
}
68