Commune   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 61.11%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 99
ccs 22
cts 36
cp 0.6111
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B find() 0 47 9
1
<?php
2
/**
3
 * TERYT-API
4
 *
5
 * Copyright (c) 2017 pudelek.org.pl
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For the full copyright and license information, please view source file
10
 * that is bundled with this package in the file LICENSE
11
 * @author  Marcin Pudełek <[email protected]>
12
 */
13
14
/**
15
 * Created by Marcin Pudełek <[email protected]>
16
 * Date: 07.09.2017
17
 */
18
19
namespace mrcnpdlk\Teryt\Model;
20
21
use mrcnpdlk\Teryt\Exception\InvalidArgument;
22
use mrcnpdlk\Teryt\Exception\NotFound;
23
use mrcnpdlk\Teryt\NativeApi;
24
25
/**
26
 * Class Commune
27
 */
28
class Commune extends EntityAbstract
29
{
30
    /**
31
     * 6 (lub 7) znakowy symbol powiatu lub tercId
32
     * sklada sie z 2 cyfr województwa , 2 powiatu, 2 gminy i 1 rodzaj gminy (opcjonalny)
33
     *
34
     * @var string
35
     */
36
    public $id;
37
    /**
38
     * Pełen identyfikator gminy
39
     *
40
     * @var string
41
     */
42
    public $tercId;
43
    /**
44
     * Nazwa powiatu
45
     *
46
     * @var string
47
     */
48
    public $name;
49
    /**
50
     * ID typu gminy
51
     *
52
     * @var string
53
     */
54
    public $typeId;
55
    /**
56
     * Nazwa typu gminy
57
     *
58
     * @var string
59
     */
60
    public $typeName;
61
    /**
62
     * Obiekt z danymi o powiecie w którym znajduje się gmina
63
     *
64
     * @var \mrcnpdlk\Teryt\Model\District
65
     */
66
    public $district;
67
68
    /**
69
     * Commune constructor.
70
     *
71
     * @param string $id 6 lub 7-znakowy symbol gminy
72
     *
73
     * @throws \mrcnpdlk\Teryt\Exception\Connection
74
     * @throws \mrcnpdlk\Teryt\Exception
75
     * @throws InvalidArgument
76
     * @throws NotFound
77
     *
78
     * @return $this
79
     */
80 1
    public function find(string $id)
81
    {
82 1
        switch (strlen($id)) {
83 1
            case 6:
84
                $provinceId = substr($id, 0, 2);
85
                $districtId = substr($id, 2, 2);
86
                $communeId  = substr($id, 2, 2);
87
                $tercId     = null;
88
                break;
89 1
            case 7:
90 1
                $provinceId = substr($id, 0, 2);
91 1
                $districtId = substr($id, 2, 2);
92 1
                $communeId  = substr($id, 2, 2);
93 1
                $tercId     = $id;
94 1
                break;
95
            default:
96
                throw new InvalidArgument(sprintf('CommuneSymbol malformed, it has %s chars', strlen($id)));
97
        }
98 1
        if (!$tercId) {
99
            foreach (NativeApi::getInstance()->PobierzListeGmin($provinceId, $districtId) as $i) {
100
                if ($i->districtId === $districtId) {
101
                    $this->id       = $id;
102
                    $this->name     = $i->name;
103
                    $this->typeName = $i->typeName;
104
                    $this->typeId   = $i->communeTypeId;
105
                    $tercId         = sprintf('%s%s%s%s', $provinceId, $districtId, $communeId, $i->communeTypeId);
106
                }
107
            }
108
        } else {
109 1
            $res = NativeApi::getInstance()->WyszukajJednostkeWRejestrze(null, NativeApi::CATEGORY_GMI_ALL, [], [$tercId]);
110 1
            if (!empty($res) && 1 === count($res)) {
111 1
                $oCommune       = $res[0];
112 1
                $this->id       = $id;
113 1
                $this->name     = $oCommune->communeName;
114 1
                $this->typeName = $oCommune->communeTypeName;
115 1
                $this->typeId   = $oCommune->communeTypeId;
116
            }
117
        }
118 1
        if (!$this->id) {
119
            throw new NotFound(sprintf('Commune [id:%s] not exists', $id));
120
        }
121
122 1
        $this->id       = sprintf('%s%s%s', $provinceId, $districtId, $communeId);
123 1
        $this->tercId   = $tercId;
124 1
        $this->district = (new District())->find(sprintf('%s%s', $provinceId, $districtId));
125
126 1
        return $this;
127
    }
128
}
129