Completed
Push — master ( f736df...73f017 )
by Beñat
05:32
created

Phone::fromInternatinal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Shared Kernel library.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\SharedKernel\Domain\Model\Phone;
13
14
use libphonenumber\NumberParseException;
15
use libphonenumber\PhoneNumberFormat;
16
use libphonenumber\PhoneNumberUtil;
17
18
/**
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
class Phone
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
22
{
23
    private $phone;
24
25
    public static function fromInternatinal($phone)
26
    {
27
        return new self($phone);
28
    }
29
30
    public static function fromRegion($region, $phone)
31
    {
32
        return new self($phone, $region);
33
    }
34
35
    public static function fromSpain($phone)
36
    {
37
        return new self($phone, 'ES');
38
    }
39
40
    private function __construct($phone, $region = null)
41
    {
42
        $this->setPhone($phone, $region);
43
    }
44
45
    public function equals(Phone $phone)
46
    {
47
        return $this->phone->phone() === $phone->phone();
0 ignored issues
show
Bug introduced by
The method phone() does not seem to exist on object<libphonenumber\PhoneNumber>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
    }
49
50
    public function phone()
51
    {
52
        return PhoneNumberUtil::getInstance()->format($this->phone, PhoneNumberFormat::E164);
53
    }
54
55
    public function phoneCallingFrom($region)
0 ignored issues
show
Unused Code introduced by
The parameter $region is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
        return PhoneNumberUtil::getInstance()->formatOutOfCountryCallingNumber($this->phoneNumber, $regionCode);
0 ignored issues
show
Bug introduced by
The property phoneNumber does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $regionCode does not exist. Did you mean $region?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
58
    }
59
60
    public function __toString()
61
    {
62
        return (string) $this->phone();
63
    }
64
65
    private function setPhone($phone, $region = null)
66
    {
67
        try {
68
            $this->phone = PhoneNumberUtil::getInstance()->parse($phone, $region);
69
            $this->checkIsValidNumber($this->phone);
70
        } catch (NumberParseException $exception) {
71
            throw new PhoneInvalidFormatException(
72
                $exception->getMessage()
73
            );
74
        }
75
    }
76
77
    private function checkIsValidNumber($phone, $region = null)
0 ignored issues
show
Unused Code introduced by
The parameter $phone is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $region is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
        if (!PhoneNumberUtil::getInstance()->isValidNumber($this->phone)) {
80
            throw new PhoneInvalidFormatException();
81
        }
82
    }
83
}
84