WriterFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
c 3
b 0
f 0
lcom 0
cbo 0
dl 0
loc 26
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 13 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
}