AbstractManager::copyRemoteFile()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 3
Metric Value
cc 3
eloc 12
c 3
b 1
f 3
nc 5
nop 2
dl 0
loc 19
rs 9.4285
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Updater Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace SWP\UpdaterBundle\Manager;
15
16
use SWP\UpdaterBundle\Client\ClientInterface;
17
use SWP\UpdaterBundle\Version\VersionInterface;
18
use vierbergenlars\SemVer\version;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
use SWP\UpdaterBundle\Model\UpdatePackage;
21
use Symfony\Component\HttpKernel\Log\LoggerInterface;
22
23
/**
24
 * Abstract update manager.
25
 */
26
abstract class AbstractManager implements ManagerInterface
27
{
28
    /**
29
     * Current app version.
30
     *
31
     * @var string
32
     */
33
    protected $currentVersion;
34
35
    /**
36
     * The latest app version.
37
     *
38
     * @var string
39
     */
40
    protected $latestVersion = '0.0.0';
41
42
    /**
43
     * @var ClientInterface
44
     */
45
    protected $client;
46
47
    /**
48
     * @var Temp directory to store update packages.
49
     */
50
    protected $tempDir;
51
52
    /**
53
     * @var App target directory.
54
     */
55
    protected $targetDir;
56
57
    /**
58
     * Logger.
59
     *
60
     * @var LoggerInterface
61
     */
62
    protected $logger;
63
64
    /**
65
     * Construct.
66
     *
67
     * @param ClientInterface      $client  Client
68
     * @param VersionInterface     $version Version
69
     * @param array                $options An array of options
70
     * @param LoggerInterface|null $logger  Logger
71
     */
72
    public function __construct(
73
        ClientInterface $client,
74
        VersionInterface $version,
75
        array $options = array(),
76
        LoggerInterface $logger = null
77
    ) {
78
        $this->client = $client;
79
        $this->currentVersion = $version->getVersion();
80
        $this->tempDir = $options['temp_dir'];
81
        $this->targetDir = $options['target_dir'];
82
        $this->logger = $logger;
83
    }
84
85
    /**
86
     * Gets the logger instance.
87
     *
88
     * @return LoggerInterface Logger
89
     */
90
    protected function getLogger()
91
    {
92
        return $this->logger;
93
    }
94
95
    /**
96
     * Has the logger instance.
97
     *
98
     * @return bool
99
     */
100
    protected function hasLogger()
101
    {
102
        if ($this->logger) {
103
            return true;
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * Adds logger info message.
111
     *
112
     * @param string $message Message
113
     */
114
    protected function addLogInfo($message)
115
    {
116
        if ($this->hasLogger()) {
117
            $this->getLogger()->info($message);
118
        }
119
    }
120
121
    /**
122
     * Copies remote file to the server where
123
     * the app is installed.
124
     *
125
     * @param string $fromUrl Remote file url
126
     * @param string $name    Copied file name
127
     *
128
     * @return bool True on success
129
     *
130
     * @throws NotFoundHttpException When file not found
131
     */
132
    protected function copyRemoteFile($fromUrl, $name)
133
    {
134
        try {
135
            $filePath = $this->tempDir.'/'.$name.'.zip';
136
            if (!file_exists($filePath)) {
137
                $this->client->saveFile($fromUrl, $filePath);
138
139
                $this->addLogInfo('Successfully downloaded update file: '.$filePath);
140
141
                return true;
142
            }
143
        } catch (\Exception $e) {
144
            throw new NotFoundHttpException(
145
                'Could not find file at the specified path: '.$fromUrl,
146
                $e,
147
                $e->getCode()
148
            );
149
        }
150
    }
151
152
    /**
153
     * Sorts an array of packages by version.
154
     * Descending order based on Semantic Versioning.
155
     *
156
     * @param array $array Array of objects
157
     */
158
    protected function sortPackagesByVersion(array $array = array())
159
    {
160
        usort($array, function ($first, $second) {
161
            if ($first instanceof UpdatePackage && $second instanceof UpdatePackage) {
162
                return version::compare($first->getVersion(), $second->getVersion());
163
            }
164
        });
165
166
        return $array;
167
    }
168
}
169