Completed
Pull Request — develop (#208)
by Franck
08:56
created

IOFactory::createWriter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation 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/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation;
19
20
/**
21
 * IOFactory
22
 */
23
class IOFactory
24
{
25
    /**
26
     * Autoresolve classes
27
     *
28
     * @var array
29
     */
30
    private static $autoResolveClasses = array('Serialized', 'ODPresentation', 'PowerPoint97', 'PowerPoint2007');
31
32
    /**
33
     * Create writer
34
     *
35
     * @param PhpPresentation $phpPresentation
36
     * @param string $name
37
     * @return \PhpOffice\PhpPresentation\Writer\WriterInterface
38
     */
39 179
    public static function createWriter(PhpPresentation $phpPresentation, $name = 'PowerPoint2007')
40
    {
41 179
        $class = 'PhpOffice\\PhpPresentation\\Writer\\' . $name;
42 179
        return self::loadClass($class, $name, 'writer', $phpPresentation);
43
    }
44
45
    /**
46
     * Create reader
47
     *
48
     * @param  string $name
49
     * @return \PhpOffice\PhpPresentation\Reader\ReaderInterface
50
     */
51 4
    public static function createReader($name = '')
52
    {
53 4
        $class = 'PhpOffice\\PhpPresentation\\Reader\\' . $name;
54 4
        return self::loadClass($class, $name, 'reader');
55
    }
56
57
    /**
58
     * Loads PhpPresentation from file using automatic \PhpOffice\PhpPresentation\Reader\ReaderInterface resolution
59
     *
60
     * @param  string        $pFilename
61
     * @return PhpPresentation
62
     * @throws \Exception
63
     */
64 3
    public static function load($pFilename)
65
    {
66
        // Try loading using self::$autoResolveClasses
67 3
        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 2
        }
73
74 1
        throw new \Exception("Could not automatically determine \PhpOffice\PhpPresentation\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\PhpPresentation\PhpPresentation $phpPresentation
84
     * @throws \Exception
85
     * @return
86
     */
87 183
    private static function loadClass($class, $name, $type, PhpPresentation $phpPresentation = null)
88
    {
89 183
        if (class_exists($class) && self::isConcreteClass($class)) {
90 182
            if (is_null($phpPresentation)) {
91 3
                return new $class();
92
            } else {
93 179
                return new $class($phpPresentation);
94
            }
95
        } else {
96 1
            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 182
    private static function isConcreteClass($class)
107
    {
108 182
        $reflection = new \ReflectionClass($class);
109
110 182
        return !$reflection->isAbstract() && !$reflection->isInterface();
111
    }
112
}
113