Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
GanttProject
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
10 / 10
34
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 canRead
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 load
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
9
 readNodeDescription
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 readNodeTasks
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 readNodeTask
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 readNodeResources
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 readNodeResource
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 readNodeAllocations
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 readNodeAllocation
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * This file is part of PHPProject - A pure PHP library for reading and writing
5 * presentations documents.
6 *
7 * PHPProject is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @link        https://github.com/PHPOffice/PHPProject
15 * @copyright   2009-2014 PHPProject contributors
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19declare(strict_types=1);
20
21namespace PhpOffice\PhpProject\Reader;
22
23use PhpOffice\PhpProject\PhpProject;
24use PhpOffice\PhpProject\Resource;
25use PhpOffice\PhpProject\Shared\XMLReader;
26use PhpOffice\PhpProject\Task;
27
28/**
29 * GanttProject
30 *
31 * @category    PHPProject
32 * @package        PHPProject
33 * @copyright    Copyright (c) 2012 - 2012 PHPProject (https://github.com/PHPOffice/PHPProject)
34 */
35class GanttProject implements ReaderInterface
36{
37    /**
38     * PHPProject object
39     *
40     * @var \PhpOffice\PhpProject\PhpProject
41     */
42    private $phpProject;
43    
44    /**
45     * Create a new GanttProject
46     */
47    public function __construct()
48    {
49        $this->phpProject = new PhpProject();
50    }
51    /**
52     *
53     * @param string $pFilename
54     * @return bool
55     */
56    public function canRead(string $pFilename): bool
57    {
58        if (file_exists($pFilename) && is_readable($pFilename)) {
59            return true;
60        }
61        return false;
62    }
63    
64    /**
65     * 
66     * @param string $pFilename
67     * @throws \Exception
68     * @return PHPProject
69     */
70    public function load(string $pFilename): PhpProject
71    {
72        if (!file_exists($pFilename) || !is_readable($pFilename)) {
73            throw new \Exception('The file is not accessible.');
74        }
75        $content = file_get_contents($pFilename);
76        $oXML = new XMLReader();
77        $oXML->getDomFromString($content);
78        
79        $oNodes = $oXML->getElements('*');
80        if ($oNodes->length > 0) {
81            foreach ($oNodes as $oNode) {
82                switch ($oNode->nodeName) {
83                    case 'allocations':
84                        $this->readNodeAllocations($oXML, $oNode);
85                        break;
86                    case 'description':
87                        $this->readNodeDescription($oNode);
88                        break;
89                    case 'resources':
90                        $this->readNodeResources($oXML, $oNode);
91                        break;
92                    case 'tasks':
93                        $this->readNodeTasks($oXML, $oNode);
94                        break;
95                }
96            }
97        }
98        
99        return $this->phpProject;
100    }
101    
102    /**
103     * Node "Description"
104     * @param \DOMElement $domNode
105     */
106    private function readNodeDescription(\DOMElement $domNode): void
107    {
108        $this->phpProject->getProperties()->setDescription($domNode->nodeValue);
109    }
110    
111    /**
112     * Node "Tasks"
113     * @param XMLReader $oXML
114     * @param \DOMElement $domNode
115     */
116    private function readNodeTasks(XMLReader $oXML, \DOMElement $domNode): void
117    {
118        $oNodes = $oXML->getElements('*', $domNode);
119        if ($oNodes->length > 0) {
120            foreach ($oNodes as $oNode) {
121                if ($oNode->nodeName == 'task') {
122                    $oTask = $this->phpProject->createTask();
123                    $this->readNodeTask($oXML, $oNode, $oTask);
124                }
125            }
126        }
127    }
128    
129    /**
130     * Node "Task"
131     * @param XMLReader $oXML
132     * @param \DOMElement $domNode
133     */
134    private function readNodeTask(XMLReader $oXML, \DOMElement $domNode, Task $oTask): void
135    {
136        // Attributes
137        $oTask->setIndex($domNode->getAttribute('id'));
138        $oTask->setName($domNode->getAttribute('name'));
139        $oTask->setStartDate($domNode->getAttribute('start'));
140        $oTask->setDuration($domNode->getAttribute('duration'));
141        $oTask->setProgress($domNode->getAttribute('complete'));
142        
143        // SubNodes
144        $oNodes = $oXML->getElements('*', $domNode);
145        if ($oNodes->length > 0) {
146            foreach ($oNodes as $oNode) {
147                if ($oNode->nodeName == 'task') {
148                    $oTaskChild = $oTask->createTask();
149                    $this->readNodeTask($oXML, $oNode, $oTaskChild);
150                }
151            }
152        }
153    }
154    
155    /**
156     * Node "Resources"
157     * @param XMLReader $oXML
158     * @param \DOMElement $domNode
159     */
160    private function readNodeResources(XMLReader $oXML, \DOMElement $domNode): void
161    {
162        $oNodes = $oXML->getElements('*', $domNode);
163        if ($oNodes->length > 0) {
164            foreach ($oNodes as $oNode) {
165                if ($oNode->nodeName == 'resource') {
166                    $oResource = $this->phpProject->createResource();
167                    $this->readNodeResource($oNode, $oResource);
168                }
169            }
170        }
171    }
172    /**
173     * Node "Resource"
174     * @param \DOMElement $domNode
175     * @param Resource $oResource
176     */
177    private function readNodeResource(\DOMElement $domNode, Resource $oResource): void
178    {
179        // Attributes
180        $oResource->setIndex($domNode->getAttribute('id'));
181        $oResource->setTitle($domNode->getAttribute('name'));
182    }
183    
184    /**
185     * Node "Allocations"
186     * @param XMLReader $oXML
187     * @param \DOMElement $domNode
188     */
189    private function readNodeAllocations(XMLReader $oXML, \DOMElement $domNode): void
190    {
191        $oNodes = $oXML->getElements('*', $domNode);
192        if ($oNodes->length > 0) {
193            foreach ($oNodes as $oNode) {
194                if ($oNode->nodeName == 'allocation') {
195                    $this->readNodeAllocation($oNode);
196                }
197            }
198        }
199    }
200    /**
201     * Node "Allocation"
202     * @param \DOMElement $domNode
203     */
204    private function readNodeAllocation(\DOMElement $domNode): void
205    {
206        // Attributes
207        $idTask = $domNode->getAttribute('task-id');
208        $idResource = $domNode->getAttribute('resource-id');
209        
210        $oResource = $this->phpProject->getResourceFromIndex($idResource);
211        $oTask = $this->phpProject->getTaskFromIndex($idTask);
212        
213        if ($oResource instanceof Resource && $oTask instanceof Task) {
214            $oTask->addResource($oResource);
215        }
216    }
217}