Api::getKrsEntity()   F
last analyzed

Complexity

Conditions 10
Paths 512

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 23
cp 0
rs 3.2187
c 0
b 0
f 0
cc 10
eloc 23
nc 512
nop 2
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * MOJEPANSTWO-API
4
 *
5
 * Copyright © 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
declare (strict_types=1);
16
17
namespace mrcnpdlk\MojePanstwo;
18
19
20
use mrcnpdlk\MojePanstwo\Model\Address\Commune;
21
use mrcnpdlk\MojePanstwo\Model\Address\District;
22
use mrcnpdlk\MojePanstwo\Model\Address\Province;
23
use mrcnpdlk\MojePanstwo\Model\KrsEntity;
24
use mrcnpdlk\MojePanstwo\Model\KrsEntityType;
25
use mrcnpdlk\MojePanstwo\Model\KrsPerson;
26
27
class Api
28
{
29
    /**
30
     * @var \mrcnpdlk\MojePanstwo\Api
31
     */
32
    protected static $instance;
33
34
    /**
35
     * @var \mrcnpdlk\MojePanstwo\Client
36
     */
37
    private $oClient;
38
39
    /**
40
     * Api constructor.
41
     *
42
     * @param \mrcnpdlk\MojePanstwo\Client $oClient
43
     */
44 1
    protected function __construct(Client $oClient)
45
    {
46 1
        $this->oClient = $oClient;
47 1
    }
48
49
    /**
50
     * @param \mrcnpdlk\MojePanstwo\Client $oClient
51
     *
52
     * @return \mrcnpdlk\MojePanstwo\Api
53
     */
54 11
    public static function create(Client $oClient): Api
55
    {
56 11
        if (null === static::$instance) {
57 1
            static::$instance = new static($oClient);
58
        }
59
60 11
        return static::$instance;
61
    }
62
63
    /**
64
     * @return \mrcnpdlk\MojePanstwo\Api
65
     * @throws \mrcnpdlk\MojePanstwo\Exception
66
     */
67 8
    public static function getInstance(): Api
68
    {
69 8
        if (null === static::$instance) {
70
            throw new Exception(sprintf('First call CREATE method!'));
71
        }
72
73 8
        return static::$instance;
74
    }
75
76
    /**
77
     * @return \mrcnpdlk\MojePanstwo\Client
78
     */
79 9
    public function getClient(): Client
80
    {
81 9
        return $this->oClient;
82
    }
83
84
    /**
85
     * @param $id
86
     *
87
     * @return Commune
88
     * @throws \mrcnpdlk\MojePanstwo\Exception
89
     */
90 1
    public function getCommune($id): Commune
91
    {
92 1
        $qb = QueryBuilder::create(Commune::class);
93
94 1
        return $qb->find((string)$id);
95
    }
96
97
    /**
98
     * @param $id
99
     *
100
     * @return District
101
     * @throws \mrcnpdlk\MojePanstwo\Exception
102
     */
103
    public function getDistrict($id): District
104
    {
105
        $qb = QueryBuilder::create(District::class);
106
107
        return $qb->find((string)$id);
108
    }
109
110
    /**
111
     * Return KRS Entity by ID (krs)
112
     *
113
     * @param string|int $krs      ID or KRS number
114
     *
115
     * @param int        $pullFlag Additional layers. E.i: KrsEntity::PULL_PKDS | KrsEntity::PULL_PERSON_REPRESENTATION
116
     *
117
     * @return \mrcnpdlk\MojePanstwo\Model\KrsEntity
118
     * @throws \mrcnpdlk\MojePanstwo\Exception
119
     */
120
    public function getKrsEntity($krs, int $pullFlag = KrsEntity::PULL_NONE): KrsEntity
121
    {
122
        $krs = (int)$krs;
123
        $qb  = QueryBuilder::create(KrsEntity::class)
124
                           ->addLayer('jedynyAkcjonariusz')
125
        ;
126
        if ($pullFlag & KrsEntity::PULL_COMPANIES) {
127
            $qb->addLayer('firmy');
128
        }
129
        if ($pullFlag & KrsEntity::PULL_DEPARTMENTS) {
130
            $qb->addLayer('oddzialy');
131
        }
132
        if ($pullFlag & KrsEntity::PULL_PARTNERS) {
133
            $qb->addLayer('wspolnicy');
134
        }
135
        if ($pullFlag & KrsEntity::PULL_PKDS) {
136
            $qb->addLayer('dzialalnosci');
137
        }
138
        if ($pullFlag & KrsEntity::PULL_SHARES) {
139
            $qb->addLayer('emisje_akcji');
140
        }
141
        if ($pullFlag & KrsEntity::PULL_PERSON_REPRESENTATION) {
142
            $qb->addLayer('reprezentacja');
143
        }
144
        if ($pullFlag & KrsEntity::PULL_PERSON_SUPERVISION) {
145
            $qb->addLayer('nadzor');
146
        }
147
        if ($pullFlag & KrsEntity::PULL_PERSON_PROXY) {
148
            $qb->addLayer('prokurenci');
149
        }
150
        if ($pullFlag & KrsEntity::PULL_PERSON_FOUNDING) {
151
            $qb->addLayer('komitetZalozycielski');
152
        }
153
154
        return $qb->find((string)$krs);
155
    }
156
157
    /**
158
     * Return KRS Entity Type
159
     *
160
     * @param $id
161
     *
162
     * @return \mrcnpdlk\MojePanstwo\Model\KrsEntityType
163
     * @throws \mrcnpdlk\MojePanstwo\Exception
164
     */
165
    public function getKrsEntityType($id): KrsEntityType
166
    {
167
        $qb = QueryBuilder::create(KrsEntityType::class);
168
169
        return $qb->find((string)$id);
170
    }
171
172
    /**
173
     * @param     $id
174
     *
175
     * @param int $pullFlag
176
     *
177
     * @return KrsPerson
178
     * @throws \mrcnpdlk\MojePanstwo\Exception
179
     */
180
    public function getKrsPerson($id, int $pullFlag = KrsPerson::PULL_NONE): KrsPerson
181
    {
182
        $qb = QueryBuilder::create(KrsPerson::class);
183
184
        /**
185
         * @var KrsPerson $oKrsPerson
186
         */
187
        $oKrsPerson = $qb->find((string)$id);
188
189
        if ($pullFlag & KrsPerson::PULL_KRS_ENTITIES) {
190
            foreach ($oKrsPerson->podmioty as $item) {
191
                $item->podmiot = $this->getKrsEntity($item->podmiot_id);
192
            }
193
        }
194
195
196
        return $oKrsPerson;
197
    }
198
199
    /**
200
     * Return Province
201
     *
202
     * @param $id
203
     *
204
     * @return Province
205
     * @throws \mrcnpdlk\MojePanstwo\Exception
206
     */
207 1
    public function getProvince($id): Province
208
    {
209 1
        $qb = QueryBuilder::create(Province::class);
210
211 1
        return $qb->find((string)$id);
212
    }
213
214
    /**
215
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
216
     * @throws \mrcnpdlk\MojePanstwo\Exception
217
     */
218 1
    public function searchCommune(): QueryBuilder
219
    {
220 1
        return QueryBuilder::create(Commune::class);
221
    }
222
223
    /**
224
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
225
     * @throws \mrcnpdlk\MojePanstwo\Exception
226
     */
227 1
    public function searchDistrict(): QueryBuilder
228
    {
229 1
        return QueryBuilder::create(District::class);
230
    }
231
232
    /**
233
     * Return QueryBuilder for searching KRS Entities
234
     *
235
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
236
     * @throws \mrcnpdlk\MojePanstwo\Exception
237
     */
238 1
    public function searchKrsEntity(): QueryBuilder
239
    {
240 1
        return QueryBuilder::create(KrsEntity::class);
241
    }
242
243
    /**
244
     * Return QueryBuilder for searching KRS Entity Types
245
     *
246
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
247
     * @throws \mrcnpdlk\MojePanstwo\Exception
248
     */
249 1
    public function searchKrsEntityType(): QueryBuilder
250
    {
251 1
        return QueryBuilder::create(KrsEntityType::class);
252
    }
253
254
    /**
255
     * Wyszukiwanie osoby w KRS
256
     *
257
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
258
     * @throws \mrcnpdlk\MojePanstwo\Exception
259
     */
260 1
    public function searchKrsPerson(): QueryBuilder
261
    {
262 1
        return QueryBuilder::create(KrsPerson::class);
263
    }
264
265
    /**
266
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
267
     * @throws \mrcnpdlk\MojePanstwo\Exception
268
     */
269 1
    public function searchProvince(): QueryBuilder
270
    {
271 1
        return QueryBuilder::create(Province::class);
272
    }
273
274
}
275