IOFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 90
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createWriter() 0 5 1
A createReader() 0 5 1
A load() 0 12 3
A loadClass() 0 12 4
A isConcreteClass() 0 6 2
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;
19
20
/**
21
 * IOFactory
22
 */
23
class IOFactory
24
{
25
    /**
26
     * Autoresolve classes
27
     *
28
     * @var array
29
     */
30
    private static $autoResolveClasses = array('GanttProject');
31
32
    /**
33
     * Create writer
34
     *
35
     * @param PhpProject $phpProject
36
     * @param string $name
37
     * @return \PhpOffice\PhpProject\Writer\WriterInterface
38
     */
39 6
    public static function createWriter(PhpProject $phpProject, $name = 'GanttProject')
40
    {
41 6
        $class = 'PhpOffice\\PhpProject\\Writer\\' . $name;
42 6
        return self::loadClass($class, $name, 'writer', $phpProject);
43
    }
44
45
    /**
46
     * Create reader
47
     *
48
     * @param  string $name
49
     * @return \PhpOffice\PhpProject\Reader\ReaderInterface
50
     */
51 4
    public static function createReader($name = 'GanttProject')
52
    {
53 4
        $class = 'PhpOffice\\PhpProject\\Reader\\' . $name;
54 4
        return self::loadClass($class, $name, 'reader');
55
    }
56
57
    /**
58
     * Loads PHPProject from file using automatic \PhpOffice\PhpProject\Reader\ReaderInterface resolution
59
     *
60
     * @param  string        $pFilename
61
     * @return PhpProject
62
     * @throws \Exception
63
     */
64 2
    public static function load($pFilename)
65
    {
66
        // Try loading using self::$autoResolveClasses
67 2
        foreach (self::$autoResolveClasses as $autoResolveClass) {
68 2
            $reader = self::createReader($autoResolveClass);
69 2
            if ($reader->canRead($pFilename)) {
70 1
                return $reader->load($pFilename);
71
            }
72 1
        }
73
74 1
        throw new \Exception("Could not automatically determine \PhpOffice\PhpProject\Reader\ReaderInterface for file.");
75
    }
76
77
    /**
78
     * Load class
79
     *
80
     * @param string $class
81
     * @param string $name
82
     * @param string $type
83
     * @param \PhpOffice\PhpProject\PhpProject $phpProject
84
     * @throws \Exception
85
     * @return
86
     */
87 10
    private static function loadClass($class, $name, $type, PhpProject $phpProject = null)
88
    {
89 10
        if (class_exists($class) && self::isConcreteClass($class)) {
90 8
            if (is_null($phpProject)) {
91 3
                return new $class();
92
            } else {
93 5
                return new $class($phpProject);
94
            }
95
        } else {
96 2
            throw new \Exception('"'.$name.'" is not a valid '.$type.'.');
97
        }
98
    }
99
100
    /**
101
     * Is it a concrete class?
102
     *
103
     * @param string $class
104
     * @return bool
105
     */
106 8
    private static function isConcreteClass($class)
107
    {
108 8
        $reflection = new \ReflectionClass($class);
109
110 8
        return !$reflection->isAbstract() && !$reflection->isInterface();
111
    }
112
}
113