MajimaController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A assign() 0 3 1
C compileAssets() 0 58 11
1
<?php
2
/**
3
 * Copyright (c) 2017
4
 *
5
 * @package   Majima
6
 * @author    David Neustadt <[email protected]>
7
 * @copyright 2017 David Neustadt
8
 * @license   MIT
9
 */
10
11
namespace Majima;
12
13
use Leafo\ScssPhp\Compiler;
14
use Majima\Services\DwooEngineFactory;
15
use Majima\Services\FluentPdoFactory;
16
use Patchwork\JSqueeze;
17
use Symfony\Component\DependencyInjection\Container;
18
use Symfony\Component\Routing\RouterInterface;
19
20
/**
21
 * Class MajimaController
22
 * @package Majima
23
 */
24
abstract class MajimaController
25
{
26
    /**
27
     * @var Container
28
     */
29
    protected $container;
30
31
    /**
32
     * @var RouterInterface
33
     */
34
    protected $router;
35
36
    /**
37
     * @var DwooEngineFactory
38
     */
39
    protected $engine;
40
41
    /**
42
     * @var FluentPdoFactory
43
     */
44
    protected $dbal;
45
46
    /**
47
     * MajimaController constructor.
48
     * @param Container $container
49
     * @param RouterInterface $router
50
     */
51
    public function __construct(Container $container, RouterInterface $router)
52
    {
53
        $this->container = $container;
54
        $this->router = $router;
55
        $this->engine = $this->container->get('dwoo.engine');
56
        $this->dbal = $this->container->get('dbal');
57
58
        if (!$this->container->get('session')->isStarted()) {
59
            $this->container->get('session')->start();
60
        }
61
62
        $this->compileAssets();
63
    }
64
65
    /**
66
     * @param array $data
67
     */
68
    public function assign($data = [])
69
    {
70
        $this->engine->setData($data);
71
    }
72
73
    protected function compileAssets()
74
    {
75
        $cssPath = BASE_DIR . join(DIRECTORY_SEPARATOR, ['web', 'css', 'style.min.css']);
0 ignored issues
show
Bug introduced by
The constant Majima\BASE_DIR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
76
        $jsPath = BASE_DIR . join(DIRECTORY_SEPARATOR, ['web', 'js', 'scripts.min.js']);
77
        $pluginCssFiles = $this->container->getParameter('plugins.css.files');
78
        $pluginJsFiles = $this->container->getParameter('plugins.js.files');
79
80
        if (!file_exists($cssPath)) {
81
            foreach ($pluginCssFiles as $viewport => $viewportCssFiles) {
82
                /** @var Compiler $compiler */
83
                $compiler = $this->container->get('scssphp.compiler');
84
85
                $importPaths = [];
86
                $scss = '';
87
88
                foreach ($viewportCssFiles as $viewportCssFile) {
89
                    if (file_exists($viewportCssFile)) {
90
                        $scss .= file_get_contents($viewportCssFile);
91
                        $importPaths[] = dirname($viewportCssFile);
92
                    }
93
                }
94
95
                $compiler->setImportPaths($importPaths);
96
                $compiler->setFormatter('Leafo\ScssPhp\Formatter\Compressed');
97
                $css = $compiler->compile($scss);
98
99
                if ($viewport == 'backend') {
100
                    $cssPath = str_replace('min.css', 'backend.min.css', $cssPath);
101
                }
102
103
                file_put_contents($cssPath, $css);
104
            }
105
        }
106
107
        if (!file_exists($jsPath)) {
108
            foreach ($pluginJsFiles as $viewport => $viewportJsFiles) {
109
                /** @var JSqueeze $compiler */
110
                $compiler = $this->container->get('jsqueeze.compiler');
111
                $rawJs = '';
112
113
                foreach ($viewportJsFiles as $viewportJsFile) {
114
                    if (file_exists($viewportJsFile)) {
115
                        $rawJs .= file_get_contents($viewportJsFile);
116
                    }
117
                }
118
119
                $minifiedJs = $compiler->squeeze(
120
                    $rawJs,
121
                    true,   // $singleLine
122
                    true,   // $keepImportantComments
123
                    true    // $specialVarRx
124
                );
125
126
                if ($viewport == 'backend') {
127
                    $jsPath = str_replace('min.js', 'backend.min.js', $jsPath);
128
                }
129
130
                file_put_contents($jsPath, $minifiedJs);
131
            }
132
        }
133
    }
134
}