WriterFactory::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 13
rs 9.4286
cc 3
eloc 7
nc 3
nop 3
1
<?php
2
3
namespace Fillet\Writer;
4
5
/**
6
 * Creates the appropriate writer class for a post type
7
 *
8
 * @package Fillet\Writer
9
 */
10
class WriterFactory
11
{
12
    /**
13
     * Create a new instance of a writer based on the post type
14
     *
15
     * @param string $postType Post type we are working against
16
     * @param array $config Config to use to generate the writer
17
     * @param bool $throwExceptionOnInvalidWriter Whether to throw an exception if an unknown writer is requested
18
     *
19
     * @return WriterInterface
20
     * @throws \Exception
21
     */
22
    public static function create($postType, $config, $throwExceptionOnInvalidWriter = false)
23
    {
24
        $className = 'Fillet\\Writer\\' . ucfirst($postType) . 'Writer';
25
        if(class_exists($className)) {
26
            /** @var WriterInterface $writer */
27
            $writer = new $className($config['destinationFolders'][$postType], $config);
28
            return $writer;
29
        }
30
31
        if($throwExceptionOnInvalidWriter) {
32
            throw new \Exception('There is no writer for ' . $postType);
33
        }
34
    }
35
}