Completed
Push — master ( 5c5133...4bc4b0 )
by Marcin
02:40
created

Api::getKrsEntity()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 22
cp 0
rs 3
c 0
b 0
f 0
cc 9
eloc 22
nc 256
nop 2
crap 90
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
26
class Api
27
{
28
    /**
29
     * @var \mrcnpdlk\MojePanstwo\Api
30
     */
31
    protected static $instance = null;
32
33
    /**
34
     * @var \mrcnpdlk\MojePanstwo\Client
35
     */
36
    private $oClient;
37
38
    /**
39
     * Api constructor.
40
     *
41
     * @param \mrcnpdlk\MojePanstwo\Client $oClient
42
     */
43 1
    protected function __construct(Client $oClient)
44
    {
45 1
        $this->oClient = $oClient;
46 1
    }
47
48
    /**
49
     * @param \mrcnpdlk\MojePanstwo\Client $oClient
50
     *
51
     * @return \mrcnpdlk\MojePanstwo\Api
52
     */
53 10
    public static function create(Client $oClient)
54
    {
55 10
        if (!isset(static::$instance)) {
56 1
            static::$instance = new static($oClient);
57
        }
58
59 10
        return static::$instance;
60
    }
61
62
    /**
63
     * @return \mrcnpdlk\MojePanstwo\Api
64
     * @throws \mrcnpdlk\MojePanstwo\Exception
65
     */
66 7
    public static function getInstance()
67
    {
68 7
        if (!isset(static::$instance)) {
69
            throw new Exception(sprintf('First call CREATE method!'));
70
        }
71
72 7
        return static::$instance;
73
    }
74
75
    /**
76
     * @return \mrcnpdlk\MojePanstwo\Client
77
     */
78 8
    public function getClient()
79
    {
80 8
        return $this->oClient;
81
    }
82
83
    /**
84
     * @param $id
85
     *
86
     * @return Commune
87
     */
88 1
    public function getCommune($id)
89
    {
90 1
        $qb = QueryBuilder::create(Commune::class);
91
92 1
        return $qb->find(strval($id));
93
    }
94
95
    /**
96
     * @param $id
97
     *
98
     * @return District
99
     */
100
    public function getDistrict($id)
101
    {
102
        $qb = QueryBuilder::create(District::class);
103
104
        return $qb->find(strval($id));
105
    }
106
107
    /**
108
     * Return KRS Entity by ID (krs)
109
     *
110
     * @param string|int $krs      ID or KRS number
111
     *
112
     * @param int        $pullFlag Additional layers. E.i: KrsEntity::PULL_PKDS | KrsEntity::PULL_PERSON_REPRESENTATION
113
     *
114
     * @return \mrcnpdlk\MojePanstwo\Model\KrsEntity
115
     */
116
    public function getKrsEntity($krs, int $pullFlag = KrsEntity::PULL_NONE)
117
    {
118
        $krs = intval($krs);
119
        $qb  = QueryBuilder::create(KrsEntity::class)
120
                           ->addLayer('jedynyAkcjonariusz')
121
                           ->addLayer('komitetZalozycielski')
122
        ;
123
        if ($pullFlag & KrsEntity::PULL_COMPANIES) {
124
            $qb->addLayer('firmy');
125
        }
126
        if ($pullFlag & KrsEntity::PULL_DEPARTMENTS) {
127
            $qb->addLayer('oddzialy');
128
        }
129
        if ($pullFlag & KrsEntity::PULL_PARTNERS) {
130
            $qb->addLayer('wspolnicy');
131
        }
132
        if ($pullFlag & KrsEntity::PULL_PKDS) {
133
            $qb->addLayer('dzialalnosci');
134
        }
135
        if ($pullFlag & KrsEntity::PULL_SHARES) {
136
            $qb->addLayer('emisje_akcji');
137
        }
138
        if ($pullFlag & KrsEntity::PULL_PERSON_REPRESENTATION) {
139
            $qb->addLayer('reprezentacja');
140
        }
141
        if ($pullFlag & KrsEntity::PULL_PERSON_SUPERVISION) {
142
            $qb->addLayer('nadzor');
143
        }
144
        if ($pullFlag & KrsEntity::PULL_PERSON_PROXY) {
145
            $qb->addLayer('prokurenci');
146
        }
147
148
        return $qb->find(strval($krs));
149
    }
150
151
    /**
152
     * Return KRS Entity Type
153
     *
154
     * @param $id
155
     *
156
     * @return \mrcnpdlk\MojePanstwo\Model\KrsEntityType
157
     */
158
    public function getKrsEntityType($id)
159
    {
160
        $qb = QueryBuilder::create(KrsEntityType::class);
161
162
        return $qb->find(strval($id));
163
    }
164
165
    /**
166
     * Return Province
167
     *
168
     * @param $id
169
     *
170
     * @return Province
171
     */
172 1
    public function getProvince($id)
173
    {
174 1
        $qb = QueryBuilder::create(Province::class);
175
176 1
        return $qb->find(strval($id));
177
    }
178
179
    /**
180
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
181
     */
182 1
    public function searchCommune()
183
    {
184 1
        return QueryBuilder::create(Commune::class);
185
    }
186
187
    /**
188
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
189
     */
190 1
    public function searchDistrict()
191
    {
192 1
        return QueryBuilder::create(District::class);
193
    }
194
195
    /**
196
     * Return QueryBuilder for searching KRS Entities
197
     *
198
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
199
     */
200 1
    public function searchKrsEntity()
201
    {
202 1
        return QueryBuilder::create(KrsEntity::class);
203
    }
204
205
    /**
206
     * Return QueryBuilder for searching KRS Entity Types
207
     *
208
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
209
     */
210 1
    public function searchKrsEntityType()
211
    {
212 1
        return QueryBuilder::create(KrsEntityType::class);
213
    }
214
215
    /**
216
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
217
     */
218 1
    public function searchProvince()
219
    {
220 1
        return QueryBuilder::create(Province::class);
221
    }
222
223
}
224