|
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; |
|
|
|
|
|
|
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
|
|
|
|
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.