|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Zippy. |
| 5 | + * |
| 6 | + |
| 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 Symfony\Component\Filesystem\Filesystem; |
| 15 | +use Symfony\Component\Filesystem\Exception\IOException as SfIOException; |
| 16 | +use Alchemy\Zippy\Exception\IOException; |
| 17 | +use Alchemy\Zippy\Resource\ResourceMapper; |
| 18 | +use Symfony\Component\Filesystem\Filesystem; |
| 19 | + |
| 20 | +/** |
| 21 | + * This class is responsible of handling resources retrievals mechanism |
| 22 | + */ |
| 23 | +class ResourceManager |
| 24 | +{ |
| 25 | + private $mapper; |
| 26 | + private $context; |
| 27 | + private $filesystem; |
| 28 | + |
| 29 | + public function __construct(ResourceMapper $mapper) |
| 30 | + { |
| 31 | + $this->mapper = $mapper; |
| 32 | + $this->filesystem = new Filesystem(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Loop throught the resource mapper and fetch resource to the approriate |
| 37 | + * context |
| 38 | + * |
| 39 | + * @return Array an array of Resource object |
| 40 | + * |
| 41 | + * @throws IOException In case temporary directory could not be created |
| 42 | + */ |
| 43 | + public function handle() |
| 44 | + { |
| 45 | + $resources = $this->mapper->map(); |
| 46 | + |
| 47 | + // default context is mapper context |
| 48 | + $this->context = $this->mapper->getContext(); |
| 49 | + |
| 50 | + if ($this->requireTemporaryDirectory()) { |
| 51 | + // change context to temporary folder |
| 52 | + $this->context = sprintf('%s/%s', sys_get_temp_dir(), uniqid('zippy_')); |
| 53 | + |
| 54 | + try { |
| 55 | + $this->filesystem->mkdir($this->context); |
| 56 | + } catch (SfIOException $e) { |
| 57 | + throw new IOException(sprintf('Could not create temporary folder %s', $this->context), $e->getCode(), $e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + $resourceCollection = array(); |
| 62 | + |
| 63 | + // teleport all resource to the appropriate context |
| 64 | + foreach ($resources as $resourceTeleporter) { |
| 65 | + $resourceCollection[] = $resourceTeleporter->teleport($this->context); |
| 66 | + } |
| 67 | + |
| 68 | + return $resourceCollection; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Remove temporary directory |
| 73 | + */ |
| 74 | + public function deleteTemporaryFiles() |
| 75 | + { |
| 76 | + if ($this->requireTemporaryDirectory() && $this->context) { |
| 77 | + try { |
| 78 | + $this->filesystem->remove($this->context); |
| 79 | + } catch (IOException $e) { |
| 80 | + |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Tells whether if the fetched resources need the creation of a temporary |
| 87 | + * folder |
| 88 | + * |
| 89 | + * @return Boolean |
| 90 | + */ |
| 91 | + public function requireTemporaryDirectory() |
| 92 | + { |
| 93 | + return count($this->mapper->getTemporaryFiles()) > 0; |
| 94 | + } |
| 95 | + |
| 96 | + public function getMapper() |
| 97 | + { |
| 98 | + return $this->mapper; |
| 99 | + } |
| 100 | + |
| 101 | + public function setMapper($mapper) |
| 102 | + { |
| 103 | + $this->mapper = $mapper; |
| 104 | + } |
| 105 | + |
| 106 | + public function getContext() |
| 107 | + { |
| 108 | + return $this->context; |
| 109 | + } |
| 110 | + |
| 111 | + public function setContext($context) |
| 112 | + { |
| 113 | + $this->context = $context; |
| 114 | + } |
| 115 | +} |
0 commit comments