| Total Complexity | 14 |
| Total Lines | 79 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
| 1 | <?php |
||
| 11 | class RegionBranchStoreUrlJson implements RegionBranchStore |
||
| 12 | { |
||
| 13 | |||
| 14 | |||
| 15 | private $url; |
||
| 16 | |||
| 17 | /** @var RegionBranch[]|null */ |
||
| 18 | private $all=null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * RegionBranchStoreUrlJson constructor. |
||
| 22 | * @param $url |
||
| 23 | */ |
||
| 24 | public function __construct(?UrlValue $url=null) |
||
| 25 | { |
||
| 26 | $this->url = $url; |
||
| 27 | if(empty($this->url)){ |
||
| 28 | $this->url=new UrlValue('https://www.rshb.ru/promo/smb/rko-partner/js/region.json'); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * @inheritDoc |
||
| 35 | * @return RegionBranch[] |
||
| 36 | */ |
||
| 37 | public function findAll(): array |
||
| 38 | { |
||
| 39 | if($this->all){ |
||
| 40 | return $this->all; |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | $resource=file_get_contents($this->url->__toString()); |
||
| 45 | if(empty($resource)){ |
||
| 46 | return []; |
||
| 47 | } |
||
| 48 | |||
| 49 | try{ |
||
| 50 | $resource=json_decode($resource,true); |
||
| 51 | if(!$resource){ |
||
| 52 | return []; |
||
| 53 | } |
||
| 54 | }catch (\Exception $e){ |
||
| 55 | return []; |
||
| 56 | } |
||
| 57 | foreach ($resource as $item){ |
||
| 58 | $this->all[]=new RegionBranch( |
||
| 59 | (int)$item['id'], |
||
| 60 | $item['origName'], |
||
| 61 | $item['newName'] |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | return $this->all; |
||
|
|
|||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritDoc |
||
| 69 | */ |
||
| 70 | public function findByRegion(string $region): ?RegionBranch |
||
| 71 | { |
||
| 72 | foreach ($this->findAll() as $regionBranch){ |
||
| 73 | if($regionBranch->getRegionName()==$region){ |
||
| 74 | return $regionBranch; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return null; |
||
| 79 | } |
||
| 80 | |||
| 81 | public function findById(int $id): ?RegionBranch |
||
| 90 | } |
||
| 91 | } |