WhatsNewController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 91
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
B get() 0 35 7
A dismiss() 0 11 2
1
<?php
2
/**
3
 * Audioplayer
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @author Arthur Schiwon <[email protected]>
10
 * @author Christoph Wurst <[email protected]>
11
 * @copyright 2020 Marcel Scherello
12
 */
13
14
namespace OCA\audioplayer\Controller;
15
16
use OCA\audioplayer\WhatsNew\WhatsNewCheck;
17
use OCP\AppFramework\Controller;
18
use OCP\AppFramework\Db\DoesNotExistException;
19
use OCP\AppFramework\Http;
20
use OCP\AppFramework\Http\DataResponse;
21
use OCP\IConfig;
22
use OCP\ILogger;
23
use OCP\IRequest;
24
use OCP\IUserSession;
25
use OCP\L10N\IFactory;
26
27
class WhatsNewController extends Controller
28
{
29
30
    /** @var IConfig */
31
    protected $config;
32
    /** @var IUserSession */
33
    private $userSession;
34
    /** @var WhatsNewCheck */
35
    private $whatsNewService;
36
    /** @var IFactory */
37
    private $langFactory;
38
    private $logger;
39
    private $AppName;
40
41
    public function __construct(
42
        string $AppName,
43
        IRequest $request,
44
        IUserSession $userSession,
45
        IConfig $config,
46
        WhatsNewCheck $whatsNewService,
47
        IFactory $langFactory,
48
        ILogger $logger
49
    )
50
    {
51
        parent::__construct($AppName, $request);
52
        $this->AppName = $AppName;
53
        $this->config = $config;
54
        $this->userSession = $userSession;
55
        $this->whatsNewService = $whatsNewService;
56
        $this->langFactory = $langFactory;
57
        $this->logger = $logger;
58
    }
59
60
    /**
61
     * @NoAdminRequired
62
     */
63
    public function get(): DataResponse
64
    {
65
        $user = $this->userSession->getUser();
66
        if ($user === null) {
67
            throw new \RuntimeException("Acting user cannot be resolved");
68
        }
69
        $lastRead = $this->config->getUserValue($user->getUID(), $this->AppName, 'whatsNewLastRead', 0);
70
        $currentVersion = $this->whatsNewService->normalizeVersion($this->config->getAppValue($this->AppName, 'installed_version'));
71
72
        if (version_compare($lastRead, $currentVersion, '>=')) {
73
            return new DataResponse([], Http::STATUS_NO_CONTENT);
74
        }
75
76
        try {
77
            $iterator = $this->langFactory->getLanguageIterator();
78
            $whatsNew = $this->whatsNewService->getChangesForVersion($currentVersion);
79
80
            $this->logger->debug(json_encode($whatsNew));
81
82
            $resultData = [
83
                'changelogURL' => $whatsNew['changelogURL'],
84
                'product' => 'Audioplayer',
85
                'version' => $currentVersion,
86
            ];
87
            do {
88
                $lang = $iterator->current();
89
                if (isset($whatsNew['whatsNew'][$lang])) {
90
                    $resultData['whatsNew'] = $whatsNew['whatsNew'][$lang];
91
                    break;
92
                }
93
                $iterator->next();
94
            } while ($lang !== 'en' && $iterator->valid());
95
            return new DataResponse($resultData);
96
        } catch (DoesNotExistException $e) {
97
            return new DataResponse([], Http::STATUS_NO_CONTENT);
98
        }
99
    }
100
101
    /**
102
     * @NoAdminRequired
103
     *
104
     * @throws \OCP\PreConditionNotMetException
105
     * @throws DoesNotExistException
106
     */
107
    public function dismiss(string $version): DataResponse
108
    {
109
        $user = $this->userSession->getUser();
110
        if ($user === null) {
111
            throw new \RuntimeException("Acting user cannot be resolved");
112
        }
113
        $version = $this->whatsNewService->normalizeVersion($version);
114
        // checks whether it's a valid version, throws an Exception otherwise
115
        $this->whatsNewService->getChangesForVersion($version);
116
        $this->config->setUserValue($user->getUID(), $this->AppName, 'whatsNewLastRead', $version);
117
        return new DataResponse();
118
    }
119
}