Completed
Push — master ( 77b81f...e54ccb )
by Konstantin
03:08
created

PassportHelper::setLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2.0185
1
<?php
2
3
namespace FormHelper;
4
5
6
class PassportHelper extends AbstractFormHelper
7
{
8
    public $passport;
9
    public $language;
10
11
    public $pass_srs;
12
    public $pass_num;
13
14
    /**
15
     * @param $passport
16
     * @return $this
17
     */
18 4
    public function set($passport)
19
    {
20 4
        $this->passport = $passport;
21
22 4
        return $this;
23
    }
24
25
    /**
26
     * @param $language
27
     * @return $this
28
     */
29 4
    public function setLanguage($language)
30
    {
31 4
        if (in_array($language, ['ru', 'ua'])) {
32 4
            $this->language = $language;
33 4
        } else {
34
            $this->language = false;
35
        }
36
37 4
        return $this;
38
    }
39
40
    /**
41
     * @return $this
42
     */
43 4
    public function explodePassport()
44
    {
45 4
        mb_internal_encoding("UTF-8");
46
47 4
        $pass_length = mb_strlen($this->passport);
48 4
        $is_valid_ru = $this->language == 'ru' && $pass_length == 10;
49 4
        $is_valid_ua = $this->language == 'ua' && $pass_length == 8;
50
51 4
        if (!($is_valid_ru || $is_valid_ua)) {
52 1
            $this->passport = $this->pass_num = $this->pass_srs = false;
53 1
            return $this;
54
        }
55
56 3 View Code Duplication
        if ($is_valid_ru) {
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...
57 1
            if ($this->isInteger($this->passport)) {
58 1
                $this->pass_srs = mb_substr($this->passport, 0, 4);
59 1
                $this->pass_num = mb_substr($this->passport, 4, 6);
60 1
            } else {
61
                $this->passport = $this->pass_num = $this->pass_srs = false;
62
            }
63 1
        }
64
65 3 View Code Duplication
        if ($is_valid_ua) {
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...
66 2
            $this->pass_srs = mb_substr($this->passport, 0, 2);
67 2
            $this->pass_num = mb_substr($this->passport, 2, 6);
68 2
            if (!($this->isText($this->pass_srs) && $this->isInteger($this->pass_num))) {
69 1
                $this->passport = $this->pass_num = $this->pass_srs = false;
70 1
            }
71 2
        }
72
73 3
        return $this;
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79 4
    public function getPassSrc()
80
    {
81 4
        return $this->pass_srs;
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87 2
    public function getPassNum()
88
    {
89 2
        return $this->pass_num;
90
    }
91
92
    /**
93
     * @return array
94
     */
95 2
    public function get()
96
    {
97 2
        return ['pass_src' => $this->pass_srs, 'pass_num' => $this->pass_num];
98
    }
99
}