SVNHelper_Command_Update::getAdded()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace AppUtils;
4
5
class SVNHelper_Command_Update extends SVNHelper_Command
6
{
7
    public const ERROR_NO_UPDATE_REVISION_SPECIFIED = 23901;
8
9
    protected function _execute()
10
    {
11
        $result = $this->execCommand('update', $this->target->getPath());
12
        
13
        if($result->isError()) {
14
            $this->throwExceptionUnexpected($result);
15
        }
16
        
17
        $this->parseResult($result);
18
        
19
        if(!isset($this->revision)) {
20
            $this->throwException(
21
                'No update revision returned',
22
                'The command did not return the expected last line with "at revision x".',
23
                self::ERROR_NO_UPDATE_REVISION_SPECIFIED,
24
                $result
25
            );
26
        }
27
        
28
        return $result;
29
    }
30
    
31
   /**
32
    * @var SVNHelper_Command_Update_Status[]
33
    */
34
    protected $stati;
35
    
36
    protected $revision;
37
    
38
   /**
39
    * Parses the command output to find out which files have been modified, and how.
40
    * @param SVNHelper_CommandResult $result
41
    */
42
    protected function parseResult(SVNHelper_CommandResult $result)
43
    {
44
        $this->stati = array();
45
        
46
        $lines = $result->getLines();
47
        
48
        foreach($lines as $line) 
49
        {
50
            $result = array();
51
            preg_match_all('/\A(a|c|d|u)[ ]+(.+)/si', $line, $result, PREG_PATTERN_ORDER);
52
            
53
            // is this a file update status line? It looks like this:
54
            // a    c:\path\to\file.ext
55
            // ^ status code
56
            if(isset($result[0]) && isset($result[1][0]) && !empty($result[1][0])) 
57
            {
58
                $status = $result[1][0];
59
                $path = $result[2][0];
60
                $obj = new SVNHelper_Command_Update_Status($this, $status, $path);
61
                
62
                if(!isset($this->stati[$status])) {
63
                    $this->stati[$status] = array();
64
                }
65
                
66
                $this->stati[$status][] = $obj;
67
                
68
                continue;
69
            }
70
            
71
            // the revision line, "updated to revision X" or "at revision X"
72
            if(strstr($line, 'revision ')) {
73
                preg_match('/(at revision|to revision) ([0-9]+)/si', $line, $result);
74
                if(isset($result[2])) {
75
                    $this->revision = $result[2];
76
                }
77
                continue;
78
            }
79
        }
80
    }
81
    
82
    public function hasConflicts()
83
    {
84
        return $this->hasStatus('c');
85
    }
86
    
87
    public function hasAdded()
88
    {
89
        return $this->hasStatus('a');
90
    }
91
    
92
    public function hasDeleted()
93
    {
94
        return $this->hasStatus('d');
95
    }
96
    
97
    public function hasUpdated()
98
    {
99
        return $this->hasStatus('u');
100
    }
101
    
102
    public function getConflicted()
103
    {
104
        return $this->getByStatus('c');
105
    }
106
    
107
    public function getUpdated()
108
    {
109
        return $this->getByStatus('u');
110
    }
111
    
112
    public function getDeleted()
113
    {
114
        return $this->getByStatus('d');
115
    }
116
    
117
    public function getAdded()
118
    {
119
        return $this->getByStatus('a');
120
    }
121
    
122
   /**
123
    * Whether there were files with the specified status code.
124
    * @param string $status
125
    * @return boolean
126
    */
127
    protected function hasStatus($status)
128
    {
129
        $this->execute();
130
        
131
        return isset($this->stati[$status]);
132
    }
133
    
134
    protected function getByStatus($status)
135
    {
136
        $this->execute();
137
        
138
        if(isset($this->stati[$status])) {
139
            return $this->stati[$status];
140
        }
141
        
142
        return array();
143
    }
144
    
145
    public function getRevision()
146
    {
147
        $this->execute();
148
        
149
        return $this->revision;
150
    }
151
}
152
153
class SVNHelper_Command_Update_Status
154
{
155
    protected $command;
156
    
157
    protected $status;
158
    
159
    protected $path;
160
    
161
    public function __construct(SVNHelper_Command_Update $command, $status, $path)
162
    {
163
        $this->command = $command;
164
        $this->status = $status;
165
        $this->path = $path;
166
    }
167
    
168
    public function getStatusCode()
169
    {
170
        return $this->status;
171
    }
172
    
173
    public function getPath()
174
    {
175
        return $this->path;
176
    }
177
    
178
    public function getFileName()
179
    {
180
        return basename($this->path);
181
    }
182
    
183
    public function getRelativePath()
184
    {
185
        return $this->getFile()->getRelativePath();
186
    }
187
    
188
    public function getFile()
189
    {
190
        return $this->command->getSVN()->getFile($this->path);
191
    }
192
}