GanttProject::readNodeTasks()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 11
cts 11
cp 1
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php
2
/**
3
 * This file is part of PHPProject - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPProject is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPProject
14
 * @copyright   2009-2014 PHPProject contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpProject\Reader;
19
20
use PhpOffice\PhpProject\PhpProject;
21
use PhpOffice\PhpProject\Resource;
22
use PhpOffice\PhpProject\Shared\XMLReader;
23
use PhpOffice\PhpProject\Task;
24
25
/**
26
 * GanttProject
27
 *
28
 * @category    PHPProject
29
 * @package        PHPProject
30
 * @copyright    Copyright (c) 2012 - 2012 PHPProject (https://github.com/PHPOffice/PHPProject)
31
 */
32
class GanttProject
33
{
34
    /**
35
     * PHPProject object
36
     *
37
     * @var \PhpOffice\PhpProject\PhpProject
38
     */
39
    private $phpProject;
40
    
41
    /**
42
     * Create a new GanttProject
43
     */
44 6
    public function __construct()
45
    {
46 6
        $this->phpProject = new PhpProject();
47 6
    }
48
    /**
49
     *
50
     * @param string $pFilename
51
     * @return PHPProject
52
     */
53 3
    public function canRead($pFilename)
54
    {
55 3
        if (file_exists($pFilename) && is_readable($pFilename)) {
56 2
            return true;
57
        }
58 2
        return false;
59
    }
60
    
61
    /**
62
     * 
63
     * @param string $pFilename
64
     * @throws \Exception
65
     * @return PHPProject
66
     */
67 3
    public function load($pFilename)
68
    {
69 3
        if (!file_exists($pFilename) || !is_readable($pFilename)) {
70 1
            throw new \Exception('The file is not accessible.');
71
        }
72 2
        $content = file_get_contents($pFilename);
73 2
        $oXML = new XMLReader();
74 2
        $oXML->getDomFromString($content);
75
        
76 2
        $oNodes = $oXML->getElements('*');
77 2
        if ($oNodes->length > 0) {
78 2
            foreach ($oNodes as $oNode) {
79 2
                switch ($oNode->nodeName) {
80 2
                    case 'allocations':
81 2
                        $this->readNodeAllocations($oXML, $oNode);
82 2
                        break;
83 2
                    case 'description':
84 1
                        $this->readNodeDescription($oNode);
85 1
                        break;
86 2
                    case 'resources':
87 2
                        $this->readNodeResources($oXML, $oNode);
88 2
                        break;
89 2
                    case 'tasks':
90 2
                        $this->readNodeTasks($oXML, $oNode);
91 2
                        break;
92 2
                }
93 2
            }
94 2
        }
95
        
96 2
        return $this->phpProject;
97
    }
98
    
99
    /**
100
     * Node "Description"
101
     * @param XMLReader $oXML
0 ignored issues
show
Bug introduced by
There is no parameter named $oXML. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
102
     * @param \DOMElement $domNode
103
     */
104 1
    private function readNodeDescription(\DOMElement $domNode)
105
    {
106 1
        $this->phpProject->getProperties()->setDescription($domNode->nodeValue);
107 1
    }
108
    
109
    /**
110
     * Node "Tasks"
111
     * @param XMLReader $oXML
112
     * @param \DOMElement $domNode
113
     */
114 2
    private function readNodeTasks(XMLReader $oXML, \DOMElement $domNode)
115
    {
116 2
        $oNodes = $oXML->getElements('*', $domNode);
117 2
        if ($oNodes->length > 0) {
118 2
            foreach ($oNodes as $oNode) {
119 2
                if ($oNode->nodeName == 'task') {
120 2
                    $oTask = $this->phpProject->createTask();
121 2
                    $this->readNodeTask($oXML, $oNode, $oTask);
122 2
                }
123 2
            }
124 2
        }
125 2
    }
126
    
127
    /**
128
     * Node "Task"
129
     * @param XMLReader $oXML
130
     * @param \DOMElement $domNode
131
     */
132 2
    private function readNodeTask(XMLReader $oXML, \DOMElement $domNode, Task $oTask)
133
    {
134
        // Attributes
135 2
        $oTask->setIndex($domNode->getAttribute('id'));
136 2
        $oTask->setName($domNode->getAttribute('name'));
137 2
        $oTask->setStartDate($domNode->getAttribute('start'));
138 2
        $oTask->setDuration($domNode->getAttribute('duration'));
139 2
        $oTask->setProgress($domNode->getAttribute('complete'));
140
        
141
        // SubNodes
142 2
        $oNodes = $oXML->getElements('*', $domNode);
143 2
        if ($oNodes->length > 0) {
144 2
            foreach ($oNodes as $oNode) {
145 2
                if ($oNode->nodeName == 'task') {
146 2
                    $oTaskChild = $oTask->createTask();
147 2
                    $this->readNodeTask($oXML, $oNode, $oTaskChild);
148 2
                }
149 2
            }
150 2
        }
151 2
    }
152
    
153
    /**
154
     * Node "Resources"
155
     * @param XMLReader $oXML
156
     * @param \DOMElement $domNode
157
     */
158 2
    private function readNodeResources(XMLReader $oXML, \DOMElement $domNode)
159
    {
160 2
        $oNodes = $oXML->getElements('*', $domNode);
161 2
        if ($oNodes->length > 0) {
162 2
            foreach ($oNodes as $oNode) {
163 2
                if ($oNode->nodeName == 'resource') {
164 2
                    $oResource = $this->phpProject->createResource();
165 2
                    $this->readNodeResource($oNode, $oResource);
166 2
                }
167 2
            }
168 2
        }
169 2
    }
170
    /**
171
     * Node "Resource"
172
     * @param XMLReader $oXML
0 ignored issues
show
Bug introduced by
There is no parameter named $oXML. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
173
     * @param \DOMElement $domNode
174
     */
175 2
    private function readNodeResource(\DOMElement $domNode, Resource $oResource)
176
    {
177
        // Attributes
178 2
        $oResource->setIndex($domNode->getAttribute('id'));
179 2
        $oResource->setTitle($domNode->getAttribute('name'));
180 2
    }
181
    
182
    /**
183
     * Node "Allocations"
184
     * @param XMLReader $oXML
185
     * @param \DOMElement $domNode
186
     */
187 2
    private function readNodeAllocations(XMLReader $oXML, \DOMElement $domNode)
188
    {
189 2
        $oNodes = $oXML->getElements('*', $domNode);
190 2
        if ($oNodes->length > 0) {
191 2
            foreach ($oNodes as $oNode) {
192 2
                if ($oNode->nodeName == 'allocation') {
193 2
                    $this->readNodeAllocation($oNode);
194 2
                }
195 2
            }
196 2
        }
197 2
    }
198
    /**
199
     * Node "Allocation"
200
     * @param XMLReader $oXML
0 ignored issues
show
Bug introduced by
There is no parameter named $oXML. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
201
     * @param \DOMElement $domNode
202
     */
203 2
    private function readNodeAllocation(\DOMElement $domNode)
204
    {
205
        // Attributes
206 2
        $idTask = $domNode->getAttribute('task-id');
207 2
        $idResource = $domNode->getAttribute('resource-id');
208
        
209 2
        $oResource = $this->phpProject->getResourceFromIndex($idResource);
210 2
        $oTask = $this->phpProject->getTaskFromIndex($idTask);
211
        
212 2
        if ($oResource instanceof Resource && $oTask instanceof Task) {
213 2
            $oTask->addResource($oResource);
214 2
        }
215 2
    }
216
}
217