Completed
Pull Request — master (#163)
by
unknown
01:42
created

ResourceManager::handle()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 4
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of Zippy.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Alchemy\Zippy\Resource;
13
14
use Alchemy\Zippy\Exception\IOException;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Symfony\Component\Filesystem\Exception\IOException as SfIOException;
17
18
class ResourceManager
19
{
20
    private $mapper;
21
    private $teleporter;
22
    private $filesystem;
23
24
    /**
25
     * Constructor
26
     *
27
     * @param RequestMapper      $mapper
28
     * @param ResourceTeleporter $teleporter
29
     * @param Filesystem         $filesystem
30
     */
31
    public function __construct(RequestMapper $mapper, ResourceTeleporter $teleporter, Filesystem $filesystem)
32
    {
33
        $this->mapper = $mapper;
34
        $this->filesystem = $filesystem;
35
        $this->teleporter = $teleporter;
36
    }
37
38
    /**
39
     * Handles an archival request.
40
     *
41
     * The request is an array of string|streams to compute in a context (current
42
     * working directory most of the time)
43
     * Some keys can be associative. In these cases, the key is used the target
44
     * for the file.
45
     *
46
     * @param string $context
47
     * @param array  $request
48
     *
49
     * @return ResourceCollection
50
     *
51
     * @throws IOException In case of write failure
52
     */
53
    public function handle($context, array $request)
54
    {
55
        $collection = $this->mapper->map($context, $request);
56
57
        if (!$collection->canBeProcessedInPlace()) {
58
            $context = sprintf('%s/%s', sys_get_temp_dir(), uniqid('zippy_'));
59
60
            try {
61
                $this->filesystem->mkdir($context);
62
            } catch (SfIOException $e) {
63
                throw new IOException(sprintf('Could not create temporary folder %s', $context), $e->getCode(), $e);
64
            }
65
66
            foreach ($collection as $resource) {
67
                $this->teleporter->teleport($context, $resource);
68
            }
69
70
            $collection = new ResourceCollection($context, $collection->toArray(), true);
71
        }
72
73
        return $collection;
74
    }
75
76
    /**
77
     * This method must be called once the ResourceCollection has been processed.
78
     *
79
     * It will remove temporary files
80
     *
81
     * @todo this should be done in the __destruct method of ResourceCollection
82
     *
83
     * @param ResourceCollection $collection
84
     */
85
    public function cleanup(ResourceCollection $collection)
86
    {
87
        if ($collection->isTemporary()) {
88
            try {
89
                $this->filesystem->remove($collection->getContext());
90
            } catch (IOException $e) {
91
                // log this ?
92
            }
93
        }
94
    }
95
96
    /**
97
     * Creates a default ResourceManager
98
     *
99
     * @return ResourceManager
100
     */
101
    public static function create()
102
    {
103
        return new static(RequestMapper::create(), ResourceTeleporter::create(), new Filesystem());
104
    }
105
}
106