Completed
Pull Request — develop (#303)
by
unknown
13:30
created

Manager::createDirectoryRecursively()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license    MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
namespace Organizations\ImageFileCache;
10
11
use Organizations\Entity\OrganizationImage;
12
use Organizations\Options\ImageFileCacheOptions as Options;
13
use Zend\Stdlib\ErrorHandler;
14
15
/**
16
 * Image file cache manager
17
 *
18
 * @author Miroslav Fedeleš <[email protected]>
19
 * @since 0.28
20
 */
21
class Manager
22
{
23
    
24
    /**
25
     * @var Options
26
     */
27
    protected $options;
28
    
29
    /**
30
     * @param Options $options
31
     */
32
    public function __construct(Options $options)
33
    {
34
        $this->options = $options;
35
    }
36
37
    /**
38
     * Returns image URI
39
     *
40
     * @param OrganizationImage $image
41
     * @return string
42
     */
43
    public function getUri(OrganizationImage $image)
44
    {
45
        return sprintf('%s/%s', $this->options->getUriPath(), $this->getImageSubPath($image));
46
    }
47
    
48
    /**
49
     * @return bool
50
     */
51
    public function isEnabled()
52
    {
53
        return $this->options->getEnabled();
54
    }
55
    
56
    /**
57
     * Store an image as a file in a file system
58
     *
59
     * @param OrganizationImage $image
60
     */
61
    public function store(OrganizationImage $image)
62
    {
63
        $resource = $image->getResource();
64
        $path = $this->getImagePath($image);
65
        $this->createDirectoryRecursively(dirname($path));
66
        file_put_contents($path, $resource);
67
    }
68
    
69
    /**
70
     * Delete an image file from file system
71
     *
72
     * @param OrganizationImage $image
73
     */
74
    public function delete(OrganizationImage $image)
75
    {
76
        @unlink($this->getImagePath($image));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
77
    }
78
79
    /**
80
     * Match the passed $uri and return an image ID on success
81
     *
82
     * @param string $uri
83
     * @return null|string Image ID
84
     */
85
    public function matchUri($uri)
86
    {
87
        $pattern = '#^' . preg_quote($this->options->getUriPath(), '#') . '/[0-9a-z]/[0-9a-z]/([0-9a-z]+)\.[a-zA-Z]{3,4}$#';
88
        $matches = [];
89
        preg_match($pattern, $uri, $matches);
90
        
91
        return isset($matches[1]) ? $matches[1] : null;
92
    }
93
    
94
    /**
95
     * @param OrganizationImage $image
96
     * @return string
97
     */
98
    protected function getImagePath(OrganizationImage $image)
99
    {
100
        return sprintf('%s/%s', $this->options->getFilePath(), $this->getImageSubPath($image));
101
    }
102
    
103
    /**
104
     * @param OrganizationImage $image
105
     * @return string
106
     */
107
    protected function getImageSubPath(OrganizationImage $image)
108
    {
109
        $id = $image->getId();
110
        $firstLevel = substr($id, -1);
111
        $secondLevel = substr($id, -2, 1);
112
        
113
        return sprintf('%s/%s/%s.%s', $firstLevel, $secondLevel, $id, pathinfo($image->getName(), PATHINFO_EXTENSION));
114
    }
115
116
    /**
117
     * @param string $dir
118
     */
119
    protected function createDirectoryRecursively($dir)
120
    {
121
        $dir = rtrim($dir, '/\\');
122
        
123
        if (! is_dir($dir)) {
124
            $this->createDirectoryRecursively(dirname($dir));
125
            
126
            $oldUmask = umask(0);
127
            
128
            ErrorHandler::start();
129
            $created = mkdir($dir, 0775);
130
            $error = ErrorHandler::stop();
131
            
132
            if (!$created) {
133
                throw new \RuntimeException(sprintf('unable to create directory "%s"', $dir), 0, $error);
134
            }
135
            
136
            umask($oldUmask);
137
        }
138
    }
139
}
140