OpenGraphGeneratorBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Novaway\Component\OpenGraph;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\CachedReader;
7
use Doctrine\Common\Annotations\Reader;
8
use Doctrine\Common\Cache\FilesystemCache;
9
use Metadata\Cache\FileCache;
10
use Metadata\MetadataFactory;
11
use Novaway\Component\OpenGraph\Builder\DefaultDriverFactory;
12
13
class OpenGraphGeneratorBuilder
14
{
15
    /** @var Reader */
16
    private $annotationReader;
17
18
    /** @var array */
19
    private $metadataDirectories;
20
21
    /** @var string */
22
    private $cacheDirectory;
23
24
    /** @var DefaultDriverFactory */
25
    private $driverFactory;
26
27
    /** @var bool */
28
    private $debug;
29
30
    /** @var bool */
31
    private $includeInterfaceMetadata;
32
33
34
    /**
35
     * Create builder instance
36
     *
37
     * @return static
38
     */
39
    public static function create()
40
    {
41
        return new static();
42
    }
43
44
    /**
45
     * Constructor
46
     */
47
    public function __construct()
48
    {
49
        $this->driverFactory = new DefaultDriverFactory();
50
51
        $this->debug = false;
52
        $this->includeInterfaceMetadata = false;
53
        $this->metadataDirectories = [];
54
    }
55
56
    /**
57
     * Create open graph object
58
     */
59
    public function build()
60
    {
61
        $annotationReader = $this->annotationReader;
62
        if (null === $annotationReader) {
63
            $annotationReader = new AnnotationReader();
64
            if (null !== $this->cacheDirectory) {
65
                $cacheDirectory = sprintf('%s/annotations', $this->cacheDirectory);
66
67
                $this->createDirectory($cacheDirectory);
68
                $annotationReader = new CachedReader($annotationReader, new FilesystemCache($cacheDirectory));
69
            }
70
        }
71
72
        $metadataDriver  = $this->driverFactory->createDriver($this->metadataDirectories, $annotationReader);
73
74
        $metadataFactory = new MetadataFactory($metadataDriver, null, $this->debug);
75
        $metadataFactory->setIncludeInterfaces($this->includeInterfaceMetadata);
76
77
        if (null !== $this->cacheDirectory) {
78
            $directory = sprintf('%s/metadata', $this->cacheDirectory);
79
80
            $this->createDirectory($directory);
81
            $metadataFactory->setCache(new FileCache($directory));
82
        }
83
84
        return new OpenGraphGenerator($metadataFactory);
85
    }
86
87
    /**
88
     * Set debug
89
     *
90
     * @param bool $debug
91
     * @return $this
92
     */
93
    public function setDebug($debug)
94
    {
95
        $this->debug = $debug;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Include the metadata from the interfaces
102
     *
103
     * @param bool $include
104
     * @return $this
105
     */
106
    public function setIncludeInterfaceMetadata($include)
107
    {
108
        $this->includeInterfaceMetadata = $include;
109
110
        return $this;
111
    }
112
113
    /**
114
     * Set annotation reader
115
     *
116
     * @param Reader $reader
117
     * @return OpenGraphGeneratorBuilder
118
     */
119
    public function setAnnotationReader(Reader $reader)
120
    {
121
        $this->annotationReader = $reader;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Set cache directory
128
     *
129
     * @param string $directory
130
     * @return OpenGraphGeneratorBuilder
131
     */
132
    public function setCacheDirectory($directory)
133
    {
134
        if (!is_dir($directory)) {
135
            $this->createDirectory($directory);
136
        }
137
138
        if (!is_writable($directory)) {
139
            throw new \InvalidArgumentException(sprintf('The cache directory "%s" is not writable.', $dir));
140
        }
141
142
        $this->cacheDirectory = $directory;
143
144
        return $this;
145
    }
146
147
    /**
148
     * Sets a map of namespace prefixes to directories.
149
     *
150
     * @param array $namespacePrefixToDirMap
151
     * @return OpenGraphGeneratorBuilder
152
     * @throws \InvalidArgumentException
153
     */
154
    public function setMetadataDirs(array $namespacePrefixToDirMap)
155
    {
156
        foreach ($namespacePrefixToDirMap as $dir) {
157
            if (!is_dir($dir)) {
158
                throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
159
            }
160
        }
161
162
        $this->metadataDirectories = $namespacePrefixToDirMap;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Adds a directory where the generator will look for class metadata.
169
     *
170
     * @param string $dir
171
     * @param string $namespacePrefix
172
     * @return OpenGraphGeneratorBuilder
173
     * @throws \InvalidArgumentException
174
     */
175 View Code Duplication
    public function addMetadataDir($dir, $namespacePrefix = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176
    {
177
        if (!is_dir($dir)) {
178
            throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
179
        }
180
181
        if (isset($this->metadataDirectories[$namespacePrefix])) {
182
            throw new \InvalidArgumentException(sprintf('There is already a directory configured for the namespace prefix "%s". Please use replaceMetadataDir() to override directories.', $namespacePrefix));
183
        }
184
185
        $this->metadataDirectories[$namespacePrefix] = $dir;
186
187
        return $this;
188
    }
189
190
    /**
191
     * Adds a map of namespace prefixes to directories.
192
     *
193
     * @param array $namespacePrefixToDirMap
194
     * @return OpenGraphGeneratorBuilder
195
     */
196
    public function addMetadataDirs(array $namespacePrefixToDirMap)
197
    {
198
        foreach ($namespacePrefixToDirMap as $prefix => $dir) {
199
            $this->addMetadataDir($dir, $prefix);
200
        }
201
202
        return $this;
203
    }
204
205
    /**
206
     * Similar to addMetadataDir(), but overrides an existing entry.
207
     *
208
     * @param string $dir
209
     * @param string $namespacePrefix
210
     * @return OpenGraphGeneratorBuilder
211
     * @throws \InvalidArgumentException
212
     */
213 View Code Duplication
    public function replaceMetadataDir($dir, $namespacePrefix = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
    {
215
        if (!is_dir($dir)) {
216
            throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
217
        }
218
219
        if (!isset($this->metadataDirectories[$namespacePrefix])) {
220
            throw new \InvalidArgumentException(sprintf('There is no directory configured for namespace prefix "%s". Please use addMetadataDir() for adding new directories.', $namespacePrefix));
221
        }
222
223
        $this->metadataDirectories[$namespacePrefix] = $dir;
224
225
        return $this;
226
    }
227
228
    /**
229
     * Create directory
230
     *
231
     * @param string $directory
232
     */
233
    private function createDirectory($directory)
234
    {
235
        if (is_dir($directory)) {
236
            return;
237
        }
238
239
        if (false === @mkdir($directory, 0777, true)) {
240
            throw new \RuntimeException(sprintf('Could not create directory "%s".', $dir));
241
        }
242
    }
243
}
244