• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

liip / LiipImagineBundle / 10701363663

04 Sep 2024 12:11PM UTC coverage: 81.688% (-0.06%) from 81.746%
10701363663

push

github

web-flow
Merge pull request #1610 from liip/cs-fix

apply latest cs fixer

67 of 84 new or added lines in 42 files covered. (79.76%)

2 existing lines in 1 file now uncovered.

2275 of 2785 relevant lines covered (81.69%)

103.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Imagine/Cache/Resolver/AbstractFilesystemResolver.php
1
<?php
2

3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11

12
namespace Liip\ImagineBundle\Imagine\Cache\Resolver;
13

14
use Liip\ImagineBundle\Binary\BinaryInterface;
15
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
16
use Liip\ImagineBundle\Imagine\Cache\CacheManagerAwareInterface;
17
use Symfony\Component\Filesystem\Exception\IOException;
18
use Symfony\Component\Filesystem\Filesystem;
19
use Symfony\Component\HttpFoundation\Request;
20

21
abstract class AbstractFilesystemResolver implements ResolverInterface, CacheManagerAwareInterface
22
{
23
    /**
24
     * @var Filesystem
25
     */
26
    protected $filesystem;
27

28
    /**
29
     * @var string
30
     */
31
    protected $basePath = '';
32

33
    /**
34
     * @var CacheManager
35
     */
36
    protected $cacheManager;
37

38
    /**
39
     * @var int
40
     */
41
    protected $folderPermissions = 0777;
42
    /**
43
     * @var Request
44
     */
45
    private $request;
46

47
    /**
48
     * Constructs a filesystem based cache resolver.
49
     */
50
    public function __construct(Filesystem $filesystem)
51
    {
52
        $this->filesystem = $filesystem;
×
53
    }
54

55
    public function setRequest(?Request $request = null)
56
    {
57
        $this->request = $request;
×
58
    }
59

60
    public function setCacheManager(CacheManager $cacheManager)
61
    {
62
        $this->cacheManager = $cacheManager;
×
63
    }
64

65
    /**
66
     * Set the base path to.
67
     */
68
    public function setBasePath($basePath)
69
    {
70
        $this->basePath = $basePath;
×
71
    }
72

73
    /**
74
     * @param int $folderPermissions
75
     */
76
    public function setFolderPermissions($folderPermissions)
77
    {
78
        $this->folderPermissions = $folderPermissions;
×
79
    }
80

81
    public function isStored($path, $filter)
82
    {
83
        return file_exists($this->getFilePath($path, $filter));
×
84
    }
85

86
    public function store(BinaryInterface $binary, $path, $filter)
87
    {
88
        $filePath = $this->getFilePath($path, $filter);
×
89

90
        $dir = pathinfo($filePath, PATHINFO_DIRNAME);
×
91

92
        $this->makeFolder($dir);
×
93

94
        file_put_contents($filePath, $binary->getContent());
×
95
    }
96

97
    public function remove(array $paths, array $filters)
98
    {
99
        if (empty($paths) && empty($filters)) {
×
100
            return;
×
101
        }
102

103
        // TODO: this logic has to be refactored.
104
        [$rootCachePath] = explode(current($filters), $this->getFilePath('whateverpath', current($filters)));
×
105

106
        if (empty($paths)) {
×
107
            $filtersCachePaths = [];
×
108
            foreach ($filters as $filter) {
×
109
                $filterCachePath = $rootCachePath.$filter;
×
110
                if (is_dir($filterCachePath)) {
×
111
                    $filtersCachePaths[] = $filterCachePath;
×
112
                }
113
            }
114

115
            $this->filesystem->remove($filtersCachePaths);
×
116

117
            return;
×
118
        }
119

120
        foreach ($paths as $path) {
×
121
            foreach ($filters as $filter) {
×
122
                $this->filesystem->remove($this->getFilePath($path, $filter));
×
123
            }
124
        }
125
    }
126

127
    /**
128
     * @throws \LogicException
129
     *
130
     * @return Request
131
     */
132
    protected function getRequest()
133
    {
134
        if (false === $this->request) {
×
135
            throw new \LogicException('The request was not injected, inject it before using resolver.');
×
136
        }
137

138
        return $this->request;
×
139
    }
140

141
    /**
142
     * @param string $dir
143
     *
144
     * @throws \RuntimeException
145
     */
146
    protected function makeFolder($dir)
147
    {
148
        if (!is_dir($dir)) {
×
149
            $parent = \dirname($dir);
×
150
            try {
151
                $this->makeFolder($parent);
×
152
                $this->filesystem->mkdir($dir);
×
153
                $this->filesystem->chmod($dir, $this->folderPermissions);
×
154
            } catch (IOException $e) {
×
NEW
155
                throw new \RuntimeException(\sprintf('Could not create directory %s', $dir), 0, $e);
×
156
            }
157
        }
158
    }
159

160
    /**
161
     * Return the local filepath.
162
     *
163
     * @param string $path   The resource path to convert
164
     * @param string $filter The name of the imagine filter
165
     *
166
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
167
     *
168
     * @return string
169
     */
170
    abstract protected function getFilePath($path, $filter);
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc