Issues (84)

src/SVNHelper/CommandError.php (1 issue)

1
<?php
2
3
namespace AppUtils;
4
5
class SVNHelper_CommandError
6
{
7
    /**
8
     * @var SVNHelper_Command
9
     */
10
    protected SVNHelper_Command $command;
11
    
12
    /**
13
     * @var string
14
     */
15
    protected string $type;
16
    
17
    /**
18
     * @var string
19
     */
20
    protected string $code;
21
    
22
    /**
23
     * @var string
24
     */
25
    protected string $message;
26
27
    /**
28
     * @param SVNHelper_Command $command
29
     * @param string $type
30
     * @param string $message
31
     * @param string|int $code
32
     */
33
    public function __construct(SVNHelper_Command $command, string $type, string $message, $code)
34
    {
35
        $this->command = $command;
36
        $this->type = $type;
37
        $this->message = $message;
38
        $this->code = $this->filterCode($code);
39
    }
40
    
41
    public function getType() : string
42
    {
43
        return $this->type;
44
    }
45
    
46
    public function isError() : bool
47
    {
48
        return $this->isType(SVNHelper_Command::SVN_ERROR_TYPE_ERROR);
49
    }
50
    
51
    public function isWarning() : bool
52
    {
53
        return $this->isType(SVNHelper_Command::SVN_ERROR_TYPE_WARNING);
54
    }
55
    
56
    public function isType(string $type) : bool
57
    {
58
        return $this->type === $type;
59
    }
60
    
61
    public function getMessage() : string
62
    {
63
        return $this->message;
64
    }
65
    
66
    public function getCode() : string
67
    {
68
        return $this->code;
69
    }
70
    
71
    public function __toString()
72
    {
73
        return sprintf(
74
            'SVN %s #%s: %s',
75
            $this->getType(),
76
            $this->getCode(),
77
            $this->getMessage()
78
        );
79
    }
80
    
81
    public function isConflict() : bool
82
    {
83
        return stristr($this->message, 'conflict');
0 ignored issues
show
Bug Best Practice introduced by
The expression return stristr($this->message, 'conflict') returns the type string which is incompatible with the type-hinted return boolean.
Loading history...
84
    }
85
    
86
    public function isLock() : bool
87
    {
88
        return $this->hasAnyErrorCode(array('155004', '195022'));
89
    }
90
    
91
    public function isConnectionFailed() : bool
92
    {
93
        return $this->hasAnyErrorCode(array('170013', '215004'));
94
    }
95
96
    /**
97
     * @param string|int $code
98
     * @return string
99
     */
100
    private function filterCode($code) : string
101
    {
102
        return ltrim((string)$code, 'e');
103
    }
104
105
    /**
106
     * @param string|int $code
107
     * @return bool
108
     */
109
    public function hasErrorCode($code) : bool
110
    {
111
        return $this->code === $this->filterCode($code);
112
    }
113
    
114
    /**
115
     * Checks whether the result has any of the error codes.
116
     * @param string[]|int[] $codes SVN style error codes, e.g. "e1234" or just the error number, e.g. "1234"
117
     * @return boolean
118
     */
119
    public function hasAnyErrorCode(array $codes) : bool
120
    {
121
        $items = array();
122
        foreach($codes as $code) {
123
            $items[] = $this->filterCode($code);
124
        }
125
        
126
        return in_array($this->code, $items, true);
127
    }
128
}