Completed
Pull Request — develop (#303)
by
unknown
12:57
created

Manager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 123
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getUri() 0 8 2
A isEnabled() 0 4 1
A store() 0 7 1
A delete() 0 4 1
A matchUri() 0 8 2
A getImagePath() 0 4 1
A getImageSubPath() 0 8 1
A createDirectoryRecursively() 0 20 3
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
        if ($this->options->getEnabled()) {
46
            return sprintf('%s/%s', $this->options->getUriPath(), $this->getImageSubPath($image));
47
        }
48
        
49
        return $image->getUri();
50
    }
51
    
52
    /**
53
     * @return bool
54
     */
55
    public function isEnabled()
56
    {
57
        return $this->options->getEnabled();
58
    }
59
    
60
    /**
61
     * Store an image as a file in a file system
62
     *
63
     * @param OrganizationImage $image
64
     */
65
    public function store(OrganizationImage $image)
66
    {
67
        $resource = $image->getResource();
68
        $path = $this->getImagePath($image);
69
        $this->createDirectoryRecursively(dirname($path));
70
        file_put_contents($path, $resource);
71
    }
72
    
73
    /**
74
     * Delete an image file from file system
75
     *
76
     * @param OrganizationImage $image
77
     */
78
    public function delete(OrganizationImage $image)
79
    {
80
        @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...
81
    }
82
83
    /**
84
     * Match the passed $uri and return an image ID on success
85
     *
86
     * @param string $uri
87
     * @return null|string Image ID
88
     */
89
    public function matchUri($uri)
90
    {
91
        $pattern = '#^' . preg_quote($this->options->getUriPath(), '#') . '/[0-9a-z]/[0-9a-z]/([0-9a-z]+)\.[a-zA-Z]{3,4}$#';
92
        $matches = [];
93
        preg_match($pattern, $uri, $matches);
94
        
95
        return isset($matches[1]) ? $matches[1] : null;
96
    }
97
    
98
    /**
99
     * @param OrganizationImage $image
100
     * @return string
101
     */
102
    protected function getImagePath(OrganizationImage $image)
103
    {
104
        return sprintf('%s/%s', $this->options->getFilePath(), $this->getImageSubPath($image));
105
    }
106
    
107
    /**
108
     * @param OrganizationImage $image
109
     * @return string
110
     */
111
    protected function getImageSubPath(OrganizationImage $image)
112
    {
113
        $id = $image->getId();
114
        $firstLevel = substr($id, -1);
115
        $secondLevel = substr($id, -2, 1);
116
        
117
        return sprintf('%s/%s/%s.%s', $firstLevel, $secondLevel, $id, pathinfo($image->getName(), PATHINFO_EXTENSION));
118
    }
119
120
    /**
121
     * @param string $dir
122
     */
123
    protected function createDirectoryRecursively($dir)
124
    {
125
        $dir = rtrim($dir, '/\\');
126
        
127
        if (! is_dir($dir)) {
128
            $this->createDirectoryRecursively(dirname($dir));
129
            
130
            $oldUmask = umask(0);
131
            
132
            ErrorHandler::start();
133
            $created = mkdir($dir, 0775);
134
            $error = ErrorHandler::stop();
135
            
136
            if (!$created) {
137
                throw new \RuntimeException(sprintf('unable to create directory "%s"', $dir), 0, $error);
138
            }
139
            
140
            umask($oldUmask);
141
        }
142
    }
143
}
144