Completed
Push — master ( db30e2...3405a3 )
by Paweł
08:38
created

OrganizationAwareThemeUploader::upload()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 10
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Theme\Uploader;
18
19
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
20
use Symfony\Component\Filesystem\Filesystem;
21
use Symfony\Component\HttpFoundation\File\UploadedFile;
22
23
/**
24
 * Class OrganizationAwareThemeUploader.
25
 */
26
final class OrganizationAwareThemeUploader implements ThemeUploaderInterface
27
{
28
    /**
29
     * @var TenantContextInterface
30
     */
31
    private $tenantContext;
32
33
    /**
34
     * @var string
35
     */
36
    private $baseDir;
37
38
    /**
39
     * OrganizationAwareThemeUploader constructor.
40
     *
41
     * @param TenantContextInterface $tenantContext
42
     * @param string                 $baseDir
43
     */
44
    public function __construct(TenantContextInterface $tenantContext, string $baseDir)
45
    {
46
        $this->tenantContext = $tenantContext;
47
        $this->baseDir = $baseDir;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function upload(UploadedFile $file)
54
    {
55
        if (null === $this->tenantContext->getTenant()) {
56
            throw new \Exception('Tenant was not found in context!');
57
        }
58
59
        $destinationFolder = $this->getAvailableThemesPath();
60
        $filePath = $file->getRealPath();
61
        $filesystem = new Filesystem();
62
63
        $zip = new \ZipArchive();
64
        if (true === $zip->open($filePath)) {
65
            if (!$filesystem->exists($destinationFolder)) {
66
                $filesystem->mkdir($destinationFolder);
67
            }
68
            $pathInArray = explode('/', $zip->getNameIndex(0));
69
            $themeDirInZip = array_shift($pathInArray);
70
71
            $themeConfiguration = $zip->getFromName($themeDirInZip.DIRECTORY_SEPARATOR.'theme.json');
72
            if (false === $themeConfiguration) {
73
                throw new \Exception('In ZIP file we expect one directory and theme.json file inside');
74
            }
75
76
            \json_decode($themeConfiguration);
77
            if (\JSON_ERROR_NONE !== json_last_error()) {
78
                throw new \Exception('Theme configuration is not valid. Syntax error in theme.json');
79
            }
80
81
            if ($filesystem->exists($destinationFolder.DIRECTORY_SEPARATOR.$themeDirInZip)) {
82
                $filesystem->remove($destinationFolder.DIRECTORY_SEPARATOR.$themeDirInZip);
83
            }
84
85
            $zip->extractTo($destinationFolder);
86
            $zip->close();
87
88
            return $destinationFolder.DIRECTORY_SEPARATOR.$themeDirInZip;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $destinationFolde...RATOR . $themeDirInZip; (string) is incompatible with the return type declared by the interface SWP\Bundle\CoreBundle\Th...loaderInterface::upload of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
89
        }
90
91
        return false;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getAvailableThemesPath()
98
    {
99
        $organizationCode = $this->tenantContext->getTenant()->getOrganization()->getCode();
100
101
        return sprintf($this->baseDir.DIRECTORY_SEPARATOR.ThemeUploaderInterface::AVAILABLE_THEMES_PATH, $organizationCode);
102
    }
103
}
104