FileSystemFactory::build()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 1
crap 4
1
<?php
2
3
namespace Partnermarketing\FileSystemBundle\Factory;
4
5
use Symfony\Component\Filesystem\Filesystem as SymfonyFileSystem;
6
use Partnermarketing\FileSystemBundle\Adapter\LocalStorage;
7
use Aws\S3\S3Client as AmazonClient;
8
use Partnermarketing\FileSystemBundle\Adapter\AmazonS3;
9
10
/**
11
 * File system factory to deliver a file storage adapter.
12
 */
13
class FileSystemFactory
14
{
15
    private $defaultFileSystem;
16
    private $config;
17
    private $tmpDir;
18
19 9
    public function __construct($defaultFileSystem, $config, $tmpDir = null)
20
    {
21 9
        $this->defaultFileSystem = $defaultFileSystem;
22 9
        $this->config = $config;
23 9
        $this->tmpDir = $tmpDir ?: sys_get_temp_dir();
24 9
    }
25
26
    /**
27
     * @return \Partnermarketing\FileSystemBundle\Adapter\AdapterInterface
28
     */
29 9
    public function build($adapterName = null)
30
    {
31 9
        if (is_null($adapterName)) {
32 4
            $adapterName = $this->defaultFileSystem;
33 4
        }
34
35
        switch ($adapterName) {
36 9
            case 'local_storage':
37 5
                return $this->buildLocalStorageFileSystem();
38 4
            case 'amazon_s3':
39 3
                return $this->buildAmazonS3FileSystem();
40 1
            default:
41 1
                throw new \Exception(
42 1
                    'The configuration for default_file_system needs to be set in the parameters.yml'
43
                    .' or the given adapter name did not match any existing file system'
44
                );
45 5
        }
46
    }
47 5
48
    private function buildLocalStorageFileSystem()
49 5
    {
50
        $fileSystem = new LocalStorage(new SymfonyFileSystem(), $this->config['local_storage'], $this->tmpDir);
51
52 3
        return $fileSystem;
53
    }
54 3
55
    private function buildAmazonS3FileSystem()
56 3
    {
57 3
        $service = new AmazonClient(array(
58 3
            'credentials' => array(
59 3
                'key'    => $this->config['amazon_s3']['key'],
60
                'secret' => $this->config['amazon_s3']['secret']
61 3
            ),
62 3
            'region' => $this->config['amazon_s3']['region'],
63
            'version' => '2006-03-01'
64 3
        ));
65
        $fileSystem = new AmazonS3($service, $this->config['amazon_s3']['bucket'], $this->tmpDir, 'public-read');
0 ignored issues
show
Unused Code introduced by
$fileSystem is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66 3
67 3
        $acl = 'public-read';
68 3
        $allowedValues = [
69 3
            'private',
70 3
            'public-read',
71
            'public-read-write',
72 3
            'authenticated-read',
73 3
            'bucket-owner-read',
74 2
            'bucket-owner-full-control'
75 1
        ];
76
        if (!empty($this->config['amazon_s3']['acl'])) {
77 1
            if (!in_array($this->config['amazon_s3']['acl'], $allowedValues)) {
78 1
                throw new \Exception('Invalid S3 acl value.');
79 2
            }
80
            $acl = $this->config['amazon_s3']['acl'];
81 2
        }
82
        $bucket = $this->config['amazon_s3']['bucket'];
83
84 2
        $fileSystem = new AmazonS3($service, $bucket, $this->tmpDir, $acl);
85
86
87
        return $fileSystem;
88
    }
89
}
90