Passed
Branch master (fcacfd)
by Anatoliy
02:23
created

RegionBranchStoreUrlJson   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
B findAll() 0 29 6
A findByRegion() 0 10 3
A findById() 0 10 3
1
<?php
2
3
4
namespace talismanfr\rosselhozbank\stores;
5
6
7
use talismanfr\rosselhozbank\dto\RegionBranch;
8
use talismanfr\rosselhozbank\shared\UrlValue;
9
use talismanfr\rosselhozbank\stores\contracts\RegionBranchStore;
10
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)
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 null;
47
        }
48
49
        try{
50
            $resource=json_decode($resource,true);
51
            if(!$resource){
52
                return null;
53
            }
54
        }catch (\Exception $e){
55
            return null;
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){
0 ignored issues
show
Bug introduced by
The expression $this->findAll() of type array<integer,object<tal...dto\RegionBranch>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
73
            if($regionBranch->getRegionName()==$region){
74
                return $regionBranch;
75
            }
76
        }
77
78
        return null;
79
    }
80
81
    public function findById(int $id): ?RegionBranch
82
    {
83
        foreach ($this->findAll() as $regionBranch){
0 ignored issues
show
Bug introduced by
The expression $this->findAll() of type array<integer,object<tal...dto\RegionBranch>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
84
            if($regionBranch->getId()==$id){
85
                return $regionBranch;
86
            }
87
        }
88
89
        return null;
90
    }
91
}