AbstractGenerator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 75
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfiguration() 0 6 1
A setFileExistsStrategy() 0 6 1
A moveOldFileIfExists() 0 17 3
A dumpToFile() 0 8 2
A getAuthorString() 0 4 1
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-06-14 
5
 */
6
7
namespace Net\Bazzline\Component\Locator\Generator;
8
9
use Net\Bazzline\Component\Locator\Configuration\Configuration;
10
use Net\Bazzline\Component\Locator\FileExistsStrategy\FileExistsStrategyInterface;
11
use Net\Bazzline\Component\Locator\Generator\GeneratorInterface;
12
use Net\Bazzline\Component\Locator\RuntimeException;
13
14
/**
15
 * Class AbstractGenerator
16
 * @package Net\Bazzline\Component\Locator
17
 */
18
abstract class AbstractGenerator implements GeneratorInterface
19
{
20
    /** @var \Net\Bazzline\Component\Locator\Configuration\Configuration */
21
    protected $configuration;
22
23
    /** @var FileExistsStrategyInterface */
24
    protected $fileExistsStrategy;
25
26
    /**
27
     * @param \Net\Bazzline\Component\Locator\Configuration\Configuration $configuration
28
     * @return $this
29
     */
30
    public function setConfiguration(Configuration $configuration)
31
    {
32
        $this->configuration = $configuration;
33
34
        return $this;
35
    }
36
37
    /**
38
     * @param FileExistsStrategyInterface $strategy
39
     * @return $this
40
     */
41
    public function setFileExistsStrategy(FileExistsStrategyInterface $strategy)
42
    {
43
        $this->fileExistsStrategy = $strategy;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $filePath
50
     * @param string $fileName
51
     * @throws RuntimeException
52
     */
53
    protected function moveOldFileIfExists($filePath, $fileName)
54
    {
55
        $fullQualifiedFilePath = $filePath . DIRECTORY_SEPARATOR . $fileName;
56
57
        if (file_exists($fullQualifiedFilePath)) {
58
            if ($this->fileExistsStrategy instanceof FileExistsStrategyInterface) {
59
                $this->fileExistsStrategy
60
                    ->setFileName($fileName)
61
                    ->setFilePath($filePath)
62
                    ->execute();
63
            } else {
64
                throw new RuntimeException(
65
                    'file "' . $fullQualifiedFilePath . '" already exists'
66
                );
67
            }
68
        }
69
    }
70
71
    /**
72
     * @param string $fullQualifiedFileName
73
     * @param string $content
74
     * @throws RuntimeException
75
     */
76
    protected function dumpToFile($fullQualifiedFileName, $content)
77
    {
78
        if (file_put_contents($fullQualifiedFileName, $content) === false) {
79
            throw new RuntimeException(
80
                'can not create "' . $fullQualifiedFileName . '" or write content'
81
            );
82
        }
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    protected function getAuthorString()
89
    {
90
        return 'Net\Bazzline\Component\Locator';
91
    }
92
}