Completed
Push — master ( 74d8cf...88d0d9 )
by Marcin
02:58
created

Commune::find()   C

Complexity

Conditions 9
Paths 10

Size

Total Lines 50
Code Lines 38

Duplication

Lines 12
Ratio 24 %

Code Coverage

Tests 22
CRAP Score 14.3967

Importance

Changes 0
Metric Value
cc 9
eloc 38
nc 10
nop 1
dl 12
loc 50
ccs 22
cts 37
cp 0.5946
crap 14.3967
rs 6
c 0
b 0
f 0
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
 *
12
 * @author  Marcin Pudełek <[email protected]>
13
 *
14
 */
15
16
/**
17
 * Created by Marcin Pudełek <[email protected]>
18
 * Date: 07.09.2017
19
 */
20
21
namespace mrcnpdlk\Teryt\Model;
22
23
24
use mrcnpdlk\Teryt\Exception\InvalidArgument;
25
use mrcnpdlk\Teryt\Exception\NotFound;
26
use mrcnpdlk\Teryt\NativeApi;
27
28
/**
29
 * Class Commune
30
 *
31
 * @package mrcnpdlk\Teryt\Model
32
 */
33
class Commune extends EntityAbstract
34
{
35
    /**
36
     * 6 (lub 7) znakowy symbol powiatu lub tercId
37
     * sklada sie z 2 cyfr województwa , 2 powiatu, 2 gminy i 1 rodzaj gminy (opcjonalny)
38
     *
39
     * @var string
40
     */
41
    public $id;
42
    /**
43
     * Pełen identyfikator gminy
44
     *
45
     * @var string
46
     */
47
    public $tercId;
48
    /**
49
     * Nazwa powiatu
50
     *
51
     * @var static
52
     */
53
    public $name;
54
    /**
55
     * ID typu gminy
56
     *
57
     * @var string
58
     */
59
    public $typeId;
60
    /**
61
     * Nazwa typu gminy
62
     *
63
     * @var string
64
     */
65
    public $typeName;
66
    /**
67
     * Obiekt z danymi o powiecie w którym znajduje się gmina
68
     *
69
     * @var \mrcnpdlk\Teryt\Model\District
70
     */
71
    public $district;
72
73
    /**
74
     * Commune constructor.
75
     *
76
     * @param string $id 6 lub 7-znakowy symbol gminy
77
     *
78
     * @return $this
79
     * @throws InvalidArgument
80
     * @throws NotFound
81
     */
82 1
    public function find(string $id)
83
    {
84 1
        switch (strlen($id)) {
85 1 View Code Duplication
            case 6;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86
                $provinceId = substr($id, 0, 2);
87
                $districtId = substr($id, 2, 2);
88
                $communeId  = substr($id, 2, 2);
89
                $tercId     = null;
90
                break;
91 1 View Code Duplication
            case 7:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
92 1
                $provinceId = substr($id, 0, 2);
93 1
                $districtId = substr($id, 2, 2);
94 1
                $communeId  = substr($id, 2, 2);
95 1
                $tercId     = $id;
96 1
                break;
97
            default:
98
                throw new InvalidArgument(sprintf('CommuneSymbol malformed, it has %s chars', strlen($id)));
99
                break;
100
        }
101 1
        if (!$tercId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tercId of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
102
            foreach (NativeApi::getInstance()->PobierzListeGmin($provinceId, $districtId) as $i) {
103
                if ($i->districtId === $districtId) {
104
                    $this->id       = $id;
105
                    $this->name     = $i->name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $i->name of type string is incompatible with the declared type mrcnpdlk\Teryt\Model\Commune of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
106
                    $this->typeName = $i->typeName;
107
                    $this->typeId   = $i->communeTypeId;
108
                    $tercId         = sprintf('%s%s%s%s', $provinceId, $districtId, $communeId, $i->communeTypeId);
109
                }
110
            }
111
112
        } else {
113 1
            $res = NativeApi::getInstance()->WyszukajJednostkeWRejestrze(null, NativeApi::CATEGORY_GMI_ALL, [], [$tercId]);
114 1
            if (!empty($res) && count($res) === 1) {
115 1
                $oCommune       = $res[0];
116 1
                $this->id       = $id;
117 1
                $this->name     = $oCommune->communeName;
118 1
                $this->typeName = $oCommune->communeTypeName;
119 1
                $this->typeId   = $oCommune->communeTypeId;
120
            }
121
        }
122 1
        if (!$this->id) {
123
            throw new NotFound(sprintf('Commune [id:%s] not exists', $id));
124
        }
125
126
127 1
        $this->id       = sprintf('%s%s%s', $provinceId, $districtId, $communeId);
128 1
        $this->tercId   = $tercId;
129 1
        $this->district = (new District())->find(sprintf('%s%s', $provinceId, $districtId));
130
131 1
        return $this;
132
    }
133
}
134