Skip to content

Commit 50008ac

Browse files
committed
Separate concerns for fetching into multiple classes to fetch streams
1 parent 2136eae commit 50008ac

File tree

10 files changed

+834
-0
lines changed

10 files changed

+834
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
/**
15+
* This class represents a resource on a local filesystem
16+
*/
17+
class Resource
18+
{
19+
/**
20+
* A path to a folder as the context
21+
*
22+
* @var String
23+
*/
24+
private $context;
25+
26+
/**
27+
* A resource relative path according to the context
28+
* @var String
29+
*/
30+
private $relativepath;
31+
32+
public function __constuct($workingDirectory, $relativepath)
33+
{
34+
$this->context = $workingDirectory;
35+
$this->relativepath = $relativepath;
36+
}
37+
38+
public function getContext()
39+
{
40+
return $this->context;
41+
}
42+
43+
public function setContext($workingDirectory)
44+
{
45+
$this->context = $workingDirectory;
46+
}
47+
48+
public function getRelativepath()
49+
{
50+
return $this->relativepath;
51+
}
52+
53+
public function setRelativepath($relativepath)
54+
{
55+
$this->relativepath = $relativepath;
56+
}
57+
58+
/**
59+
* Gets the full resource path
60+
*
61+
* @return String
62+
*/
63+
public function getPath()
64+
{
65+
return sprintf('%s/%s', rtrim($this->context, '/'), $this->relativepath);
66+
}
67+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 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

Comments
 (0)