DatabaseNotificationGenerator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Generator;
4
5
use Sensio\Bundle\GeneratorBundle\Generator\Generator;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
8
9
/**
10
 * Generates a Notification inside a bundle.
11
 */
12
class DatabaseNotificationGenerator extends Generator
13
{
14
    /** @var Filesystem */
15
    private $filesystem;
16
17
    public function __construct(Filesystem $filesystem)
18
    {
19
        $this->filesystem = $filesystem;
20
    }
21
22
    /**
23
     * @param BundleInterface $bundle
24
     * @param                 $entityName
25
     */
26
    public function generate(BundleInterface $bundle, $entityName)
27
    {
28
        $bundleDir = $bundle->getPath();
29
        $notificationDir = $bundleDir . '/Entity';
30
        self::mkdir($notificationDir);
31
32
        $notificationFile = $notificationDir . '/' . $entityName . '.php';
33
34
        // Check that each file does not already exist
35
        if ($this->filesystem->exists($notificationFile)) {
36
            throw new \RuntimeException(sprintf('"%s" already exists', $notificationFile));
37
        }
38
39
        $parameters = [
40
            'namespace' => $bundle->getNamespace(),
41
            'class_name' => $entityName,
42
        ];
43
44
        // Set generator to look in correct directory for notifications template.
45
        $path = __DIR__ . '/../Resources/skeleton';
46
        $this->setSkeletonDirs([$path]);
47
48
        // Template, destination, params
49
        $this->renderFile('notification/DatabaseNotification.php.twig', $notificationFile, $parameters);
50
    }
51
}