NotificationGenerator::generateFiles()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 5
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 NotificationGenerator extends Generator
13
{
14
    /** @var Filesystem */
15
    private $filesystem;
16
    private $channels;
17
    private $rootDirectory;
18
19
    /**
20
     * NotificationGenerator constructor.
21
     *
22
     * @param Filesystem $filesystem
23
     */
24
    public function __construct(Filesystem $filesystem, array $channels, $rootDirectory)
25
    {
26
        $this->filesystem = $filesystem;
27
        $this->channels = $channels;
28
        $this->rootDirectory = $rootDirectory;
29
    }
30
31
    /**
32
     * @param BundleInterface $bundle
33
     * @param                 $name
34
     */
35
    public function generate(BundleInterface $bundle, $name)
36
    {
37
        $bundleDir = $bundle->getPath();
38
        $notificationDir = $bundleDir . '/Notification';
39
        self::mkdir($notificationDir);
40
41
        $notificationClassName = $name . 'Notification';
42
        $notificationFile = $notificationDir . '/' . $notificationClassName . '.php';
43
44
        $parameters = [
45
            'namespace' => $bundle->getNamespace(),
46
            'class_name' => $notificationClassName,
47
            'name' => $name,
48
            'channels' => $this->channels,
49
        ];
50
51
        // Build an array of files to be created
52
        $filesArray = [];
53
        $filesArray[] = [
54
            'notification/Notification.php.twig',
55
            $notificationFile,
56
            $parameters,
57
        ];
58
59
        // Generate the templates for each channel.
60
        // A general message.twig and a title.twig should also be generated
61
        $templateDir = $this->rootDirectory . '/Resources/NotificationBundle/views/' . $name . '/';
62
63
        $filesArray[] = ['message/body.html.twig', $templateDir . 'body.html.twig', []];
64
        $filesArray[] = ['message/title.html.twig', $templateDir . 'title.html.twig', []];
65
66
        foreach ($this->channels as $channel) {
67
            $destination = $templateDir . $channel . '.message.html.twig';
68
            $filesArray[] = [
69
                'message/' . $channel . '.message.html.twig',
70
                $destination,
71
                [],
72
            ];
73
        }
74
75
        if (!empty($filesArray)) {
76
            $this->generateFiles($filesArray);
77
        }
78
    }
79
80
    protected function generateFiles(array $files)
81
    {
82
        // Set generator to look in correct directory for notifications template.
83
        $path = __DIR__ . '/../Resources/skeleton';
84
        $this->setSkeletonDirs([$path]);
85
86
        // Check that each file does not already exist
87
        foreach ($files as $file) {
88
            if ($this->filesystem->exists($file[1])) {
89
                throw new \RuntimeException(sprintf('"%s" already exists', $file[1]));
90
            }
91
        }
92
93
        // Generate each file
94
        foreach ($files as $file) {
95
            // Template, destination, params
96
            $this->renderFile($file[0], $file[1], $file[2]);
97
        }
98
    }
99
}