PageBreakpointService::getBreakpointTemplateHash()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Columnis\Service;
4
5
use Columnis\Model\PageBreakpoint;
6
use \Smarty;
7
use \CssMin;
8
9
class PageBreakpointService {
10
11
    /**
12
     * @var array The templates paths
13
     */
14
    protected $templatesPathStack = array();
15
16
    /**
17
     * @var array The assets manager paths
18
     */
19
    protected $assetsManagerPathStack = array();
20
21
    /**
22
     * Smarty object
23
     * 
24
     * @var Smarty 
25
     */
26
    protected $smarty;
27
28
    /**
29
     * Retrieve paths to templates
30
     *
31
     * @return array
32
     */
33
    public function getTemplatesPathStack() {
34
        return $this->templatesPathStack;
35
    }
36
37
    /**
38
     * Set the templates paths
39
     *
40
     * @param array $templatesPathStack
41
     */
42
    public function setTemplatesPathStack(Array $templatesPathStack) {
43
        $this->templatesPathStack = $templatesPathStack;
44
    }
45
46
    /**
47
     * Retrieve paths to assets manager
48
     * 
49
     * @return array
50
     */
51
    function getAssetsManagerPathStack() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
        return $this->assetsManagerPathStack;
53
    }
54
55
    /**
56
     * Set the assets manager paths
57
     * 
58
     * @param array $assetsManagerPathStack
59
     */
60
    function setAssetsManagerPathStack($assetsManagerPathStack) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
61
        $this->assetsManagerPathStack = $assetsManagerPathStack;
62
    }
63
64
    /**
65
     * Return Smarty Object
66
     * 
67
     * @return Smarty
68
     */
69
    function getSmarty() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
70
        return $this->smarty;
71
    }
72
73
    /**
74
     * Set the Smarty Object
75
     * @param Smarty $smarty
76
     */
77
    function setSmarty(Smarty $smarty) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
78
        $this->smarty = $smarty;
79
    }
80
81
    /**
82
     * Constructor
83
     * 
84
     * @param Smarty $smarty
85
     * @param array $templatesPathStack
86
     * @param array $assetsManagerPathStack
87
     */
88
    function __construct(Smarty $smarty, $templatesPathStack, $assetsManagerPathStack) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89
        $this->smarty = $smarty;
90
        $this->templatesPathStack = $templatesPathStack;
91
        $this->assetsManagerPathStack = $assetsManagerPathStack;
92
    }
93
94
    /**
95
     * Creats a PageBreakpoint file if not exist
96
     *
97
     * @param int $idPage
98
     * @param array $extraData
99
     * @param string $hash
100
     * @param array $images
101
     * @param array $imageSizesGroups
102
     * @return string
103
     */
104
    public function createPageBreakpoint($idPage, $extraData, $hash, $images, $imageSizesGroups) {
105
        $pageBreakpoint = new PageBreakpoint();
106
        $pageBreakpoint->setHash($hash);
107
        $pageBreakpoint->setIdPage($idPage);
108
        $pageBreakpoint->setExtraData($extraData);
109
        $pageBreakpoint->setImages($images);
110
        $pageBreakpoint->setTemplateHash($this->getBreakpointTemplateHash());
111
        $pageBreakpoint->setImageGroupsSizes($imageSizesGroups);
112
        $this->loadBreakpointsPath($pageBreakpoint); //Sets path
113
        $pathExist = $this->checkBreakpointPath($pageBreakpoint); //Check path exist, if not create it
114
        $currentBreakpointName = $this->getCurrentBreakpointFilename($idPage, $pageBreakpoint);
115
        $breakpointChange = $currentBreakpointName !== $pageBreakpoint->getFileName();
116
        if(!$pathExist || $breakpointChange) {
117
            //Invalidate last file
118
            $this->invalidateCurrentBreakpointFile($pageBreakpoint, $currentBreakpointName);
0 ignored issues
show
Security Bug introduced by
It seems like $currentBreakpointName defined by $this->getCurrentBreakpo...dPage, $pageBreakpoint) on line 114 can also be of type false; however, Columnis\Service\PageBre...CurrentBreakpointFile() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
119
            //Create it if path not exist (css/breakpoint dir) or the current file are diferent with the parameters
120
            $this->createBreakpointFile($pageBreakpoint);
121
        }
122
        return $pageBreakpoint->getFileName();
123
    }
124
125
    /**
126
     * Sets the path to pageBreakpoint object
127
     * 
128
     * @param PageBreakpoint $pageBreakpoint
129
     */
130
    protected function loadBreakpointsPath(PageBreakpoint &$pageBreakpoint) {
131
        $assetsPaths = $this->getAssetsManagerPathStack();
132
        if(is_array($assetsPaths)) {
133
            $pageBreakpoint->setPath($assetsPaths[1]);
134
        }
135
    }
136
137
    /**
138
     * Check Breakpoints directory, if not exist create it
139
     * 
140
     * @param PageBreakpoint $pageBreakpoint
141
     * @return boolean
142
     */
143
    protected function checkBreakpointPath(PageBreakpoint &$pageBreakpoint) {
144
        $path = $pageBreakpoint->getFullPath();
145
        if(!file_exists($path)) {
146
            mkdir($path);
147
            return false;
148
        }
149
        return true;
150
    }
151
152
    /**
153
     * Search on breakpoints dir the current breakpoint page file
154
     * 
155
     * @param int $idPage
156
     * @param PageBreakpoint $pageBreakpoint
157
     * @return boolean
158
     */
159
    protected function getCurrentBreakpointFilename($idPage, PageBreakpoint $pageBreakpoint) {
160
        $breakpointFiles = glob($pageBreakpoint->getFullPath().$idPage."-*.css");
161
        if(is_array($breakpointFiles) && count($breakpointFiles)) {
162
            $breakpointFile = basename(array_pop($breakpointFiles));
163
            return $breakpointFile;
164
        }
165
        return false;
166
    }
167
168
    /**
169
     * Get the template file hash
170
     * 
171
     * @return string
172
     */
173
    protected function getBreakpointTemplateHash() {
174
        $templatesPath = $this->getTemplatesPathStack()[1];
175
        $templateFile = $templatesPath.DIRECTORY_SEPARATOR.PageBreakpoint::BREAKPOINT_FILE;
176
        if(file_exists($templateFile)) {
177
            $hash = md5_file($templateFile);
178
        }
179
        return $hash;
0 ignored issues
show
Bug introduced by
The variable $hash does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
180
    }
181
182
    /**
183
     * Create a new PageBreakpointFile
184
     * 
185
     * @param PageBreakpoint $pageBreakpoint
186
     */
187
    protected function createBreakpointFile(PageBreakpoint $pageBreakpoint) {
188
        try {
189
            //TemplatesConfig
190
            $templatesPath = $this->getTemplatesPathStack()[1];
191
            if(file_exists($templatesPath.DIRECTORY_SEPARATOR.PageBreakpoint::BREAKPOINT_FILE)) {
192
                //Get smarty object
193
                $smarty = $this->getSmarty();
194
                //Set templates path
195
                $smarty->setTemplateDir($templatesPath);
196
                //Assign data
197
                $smarty->assign('data', $pageBreakpoint->getData());
198
                //Fetch content
199
                $breakpointContent = $smarty->fetch(PageBreakpoint::BREAKPOINT_FILE);
200
                // write to file 
201
                file_put_contents($pageBreakpoint->getFullFileName(), CssMin::minify($breakpointContent));
202
            }
203
        } catch(\Exception $exc) {
204
            
205
        }
206
    }
207
208
    /**
209
     * Remove the last breakpoint file
210
     * 
211
     * @param PageBreakpoint $pageBreakpoint
212
     * @param string $currentFileName
213
     */
214
    protected function invalidateCurrentBreakpointFile(PageBreakpoint $pageBreakpoint, $currentFileName) {
215
        try {
216
            //Parse full current breakpoint name
217
            $invalidatePath = $pageBreakpoint->getFullPath().$currentFileName;
218
            //If exist delete it
219
            if(file_exists($invalidatePath) && is_file($invalidatePath)) {
220
                unlink($invalidatePath);
221
            }
222
        } catch(\Exception $exc) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
223
            
224
        }
225
    }
226
}
227