GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( e30b6b...94d2c2 )
by Orlando
01:37
created

Certificate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 98
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getStrategy() 0 17 3
A decode() 0 4 1
A save() 0 10 1
A __call() 0 10 2
A getFileExtensionName() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the cfdi-certificate project.
5
 *
6
 * (c) Kinedu
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 Kinedu\CFDI\Certificate;
13
14
use Exception;
15
use Kinedu\CFDI\Certificate\IO;
16
use Kinedu\CFDI\Certificate\Strategies\CerStrategy;
17
use Kinedu\CFDI\Certificate\Strategies\KeyStrategy;
18
19
class Certificate
20
{
21
    /**
22
     * File to decode.
23
     *
24
     * @var string
25
     */
26
    protected $file;
27
28
    /**
29
     * Password to the decode the file.
30
     *
31
     * @var string
32
     */
33
    protected $password;
34
35
    /** @var string */
36
    protected $strategy;
37
38
    /**
39
     * Create a new certificate instance.
40
     *
41
     * @param string $file
42
     * @param string $password
43
     * @param string $strategy
44
     */
45
    public function __construct(string $file, string $password = null, string $strategy = null)
46
    {
47
        $this->file = new IO($file);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Kinedu\CFDI\Certificate\IO($file) of type object<Kinedu\CFDI\Certificate\IO> is incompatible with the declared type string of property $file.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
49
        $this->password = $password;
50
51
        $this->strategy = $strategy;
52
    }
53
54
    /**
55
     * @return CerStrategy|KeyStrategy|null
56
     */
57
    protected function getStrategy()
58
    {
59
        switch ($this->getFileExtensionName()) {
60
            case 'cer':
61
                return new CerStrategy(
62
                    $this->file->getOriginalRoute()
0 ignored issues
show
Bug introduced by
The method getOriginalRoute cannot be called on $this->file (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
63
                );
64
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
65
66
            case 'key':
67
                return new KeyStrategy(
68
                    $this->file->getOriginalRoute(),
0 ignored issues
show
Bug introduced by
The method getOriginalRoute cannot be called on $this->file (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
69
                    $this->password
70
                );
71
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
72
        }
73
    }
74
75
    public function decode(): string
76
    {
77
        return $this->getStrategy()->decode();
78
    }
79
80
    /**
81
     * @param string $directory
82
     * @param string $filename
83
     *
84
     * @return integer|bool
85
     */
86
    public function save(string $directory, string $filename = null)
87
    {
88
        $filename  = $filename ?? $this->file->getFileName();
0 ignored issues
show
Bug introduced by
The method getFileName cannot be called on $this->file (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
89
        $extension = $this->getFileExtensionName();
90
91
        $directory = rtrim($directory, '/').'/';
92
        $directory = "{$directory}{$filename}.{$extension}.pem";
93
94
        return file_put_contents($directory, $this->decode());
95
    }
96
97
    /**
98
     * @param string $name
99
     * @param array $arguments
100
     */
101
    public function __call(string $name, array $arguments)
102
    {
103
        $strategy = $this->getStrategy();
104
105
        if (method_exists($strategy, $name)) {
106
            return $strategy->{$name}($arguments);
107
        } else {
108
            throw new Exception("This method doesn't exist");
109
        }
110
    }
111
112
    protected function getFileExtensionName()
113
    {
114
        return $this->strategy ?? $this->file->getFileExtensionName();
0 ignored issues
show
Bug introduced by
The method getFileExtensionName cannot be called on $this->file (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
115
    }
116
}
117