Completed
Pull Request — development (#3)
by Philippe
04:45 queued 02:15
created

AccountController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * This program is free software: you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation, either version 3 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 */
16
17
namespace Filoucrackeur\Hubic\Controller\Backend;
18
19
use Filoucrackeur\Hubic\Domain\Model\Account;
20
use Filoucrackeur\Hubic\Service\HubicService;
21
use TYPO3\CMS\Core\Messaging\AbstractMessage;
22
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
23
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
24
25
/**
26
 * Class AccountController
27
 */
28
class AccountController extends ActionController
29
{
30
    /**
31
     * @var \Filoucrackeur\Hubic\Service\HubicService
32
     */
33
    protected $hubicService;
34
35 1
    public function indexAction()
36
    {
37 1
        $this->view->assign('accounts', $this->hubicService->getAccounts());
38 1
    }
39
40
    /**
41
     * @param Account $account
42
     */
43
    public function showAction(Account $account)
44
    {
45
        try {
46
            if ($account->getAccessToken()) {
47
                $this->hubicService->setAccount($account);
48
                $this->view->assignMultiple([
49
                    'clientAccount' => $this->hubicService->getAccount(),
50
                    'clientAccountQuota' => $this->hubicService->getAccountQuota(),
51
                    'agreement' => $this->hubicService->getAgreement(),
52
                    'links' => $this->hubicService->getAllLinks()
53
                ]);
54
            }
55
56
            $this->view->assign('account', $account);
57
        } catch (\Exception $e) {
58
            $this->forward('index');
59
        }
60
    }
61
62
    /**
63
     * @param Account $account
64
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
65
     */
66 1
    public function refreshTokenAction(Account $account)
67
    {
68 1
        $this->hubicService->refreshToken($account);
69
70 1
        $this->forward('show', null, null, ['account' => $account]);
71 1
    }
72
73
    /**
74
     * @param Account $account
75
     */
76 1
    public function accessTokenAction(Account $account)
77
    {
78
        try {
79 1
            $this->hubicService->redirectUrlRequestToken($account);
80
81
        } catch (\Exception $e) {
82
            $this->forward('index');
83
        }
84 1
    }
85
86
    /**
87
     * @param Account $account
88
     * @throws \RuntimeException
89
     * @throws \InvalidArgumentException
90
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
91
     */
92
    public function callbackAction(Account $account)
93
    {
94
        try {
95
96
            if ($this->hubicService->accessToken($account)) {
97
                $this->addFlashMessage(LocalizationUtility::translate('flashmessage.token_added', 'hubic'),
98
                    LocalizationUtility::translate('flashmessage.authentication', 'hubic'),
99
                    AbstractMessage::OK);
100
            } else {
101
                $this->addFlashMessage(LocalizationUtility::translate('flashmessage.missing_client_data', 'hubic'),
102
                    LocalizationUtility::translate('flashmessage.authentication', 'hubic'),
103
                    AbstractMessage::ERROR);
104
            }
105
            $this->forward('show', null, null, ['account' => $account]);
106
107
        } catch (\Exception $e) {
108
            $this->addFlashMessage($e->getMessage(),'', AbstractMessage::ERROR);
109
            $this->forward('index');
110
        }
111
    }
112
113
    /**
114
     * @param Account $account
115
     * @throws \InvalidArgumentException
116
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
117
     */
118
    public function deleteAction(Account $account)
119
    {
120
        $this->hubicService->delete($account);
121
        $this->addFlashMessage(LocalizationUtility::translate('flashmessage.account_deleted', 'hubic'),
122
            LocalizationUtility::translate('account', 'hubic'),
123
            AbstractMessage::OK);
124
        $this->forward('index');
125
    }
126
127
    /**
128
     * @param Account $account
129
     * @throws \InvalidArgumentException
130
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
131
     */
132
    public function unlinkAction(Account $account)
133
    {
134
        $this->hubicService->unlink($account);
135
136
        $this->addFlashMessage(LocalizationUtility::translate('flashmessage.account_unlinked', 'hubic'),
137
            LocalizationUtility::translate('account', 'hubic'),
138
            AbstractMessage::OK);
139
        $this->forward('index');
140
    }
141
142
    /**
143
     * @param Account $account
144
     * @param string $uri
145
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
146
     */
147
    public function unlinkUriAction(Account $account, string $uri)
148
    {
149
        $this->hubicService->deleteLink($account, $uri);
150
        $this->forward('show', null, null, ['account' => $account]);
151
    }
152
153
    /**
154
     * @param HubicService $hubicService
155
     */
156 3
    public function injectHubicService(HubicService $hubicService)
157
    {
158 3
        $this->hubicService = $hubicService;
159 3
    }
160
}
161