WhatsNewController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
dl 0
loc 88
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dismiss() 0 11 2
A __construct() 0 17 1
B get() 0 33 7
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors, 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Controller;
10
11
use OCA\Analytics\WhatsNew\WhatsNewCheck;
12
use OCP\AppFramework\Controller;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use OCP\AppFramework\Db\DoesNotExistException;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Db\DoesNotExistException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use OCP\AppFramework\Http;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use OCP\AppFramework\Http\DataResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\DataResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use OCP\IConfig;
0 ignored issues
show
Bug introduced by
The type OCP\IConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use OCP\IRequest;
0 ignored issues
show
Bug introduced by
The type OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use OCP\IUserSession;
0 ignored issues
show
Bug introduced by
The type OCP\IUserSession was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use OCP\L10N\IFactory;
0 ignored issues
show
Bug introduced by
The type OCP\L10N\IFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
class WhatsNewController extends Controller
23
{
24
25
    /** @var IConfig */
26
    protected $config;
27
    /** @var IUserSession */
28
    private $userSession;
29
    /** @var WhatsNewCheck */
30
    private $whatsNewService;
31
    /** @var IFactory */
32
    private $langFactory;
33
    private $logger;
34
35
    public function __construct(
36
        string $appName,
37
        IRequest $request,
38
        IUserSession $userSession,
39
        IConfig $config,
40
        WhatsNewCheck $whatsNewService,
41
        IFactory $langFactory,
42
        LoggerInterface $logger
43
    )
44
    {
45
        parent::__construct($appName, $request);
46
        $this->appName = $appName;
0 ignored issues
show
Bug Best Practice introduced by
The property appName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
        $this->config = $config;
48
        $this->userSession = $userSession;
49
        $this->whatsNewService = $whatsNewService;
50
        $this->langFactory = $langFactory;
51
        $this->logger = $logger;
52
    }
53
54
    /**
55
     * @NoAdminRequired
56
     */
57
    public function get(): DataResponse
58
    {
59
        $user = $this->userSession->getUser();
60
        if ($user === null) {
61
            throw new \RuntimeException("Acting user cannot be resolved");
62
        }
63
        $lastRead = $this->config->getUserValue($user->getUID(), $this->appName, 'whatsNewLastRead', 0);
64
        $currentVersion = $this->whatsNewService->normalizeVersion($this->config->getAppValue($this->appName, 'installed_version'));
65
66
        if (version_compare($lastRead, $currentVersion, '>=')) {
67
            return new DataResponse([], Http::STATUS_NO_CONTENT);
68
        }
69
70
        try {
71
            $iterator = $this->langFactory->getLanguageIterator();
72
            $whatsNew = $this->whatsNewService->getChangesForVersion($currentVersion);
73
74
            $resultData = [
75
                'changelogURL' => $whatsNew['changelogURL'],
76
                'product' => 'Analytics',
77
                'version' => $currentVersion,
78
            ];
79
            do {
80
                $lang = $iterator->current();
81
                if (isset($whatsNew['whatsNew'][$lang])) {
82
                    $resultData['whatsNew'] = $whatsNew['whatsNew'][$lang];
83
                    break;
84
                }
85
                $iterator->next();
86
            } while ($lang !== 'en' && $iterator->valid());
87
            return new DataResponse($resultData);
88
        } catch (DoesNotExistException $e) {
89
            return new DataResponse([], Http::STATUS_NO_CONTENT);
90
        }
91
    }
92
93
    /**
94
     * @NoAdminRequired
95
     *
96
     * @throws \OCP\PreConditionNotMetException
97
     * @throws DoesNotExistException
98
     */
99
    public function dismiss(string $version): DataResponse
100
    {
101
        $user = $this->userSession->getUser();
102
        if ($user === null) {
103
            throw new \RuntimeException("Acting user cannot be resolved");
104
        }
105
        $version = $this->whatsNewService->normalizeVersion($version);
106
        // checks whether it's a valid version, throws an Exception otherwise
107
        $this->whatsNewService->getChangesForVersion($version);
108
        $this->config->setUserValue($user->getUID(), $this->appName, 'whatsNewLastRead', $version);
109
        return new DataResponse();
110
    }
111
}