DefaultBoardService::createDefaultBoard()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.5066
c 0
b 0
f 0
cc 7
nc 4
nop 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Ryan Fletcher <[email protected]>
4
 *
5
 * @author Ryan Fletcher <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Service;
25
26
use OCA\Deck\AppInfo\Application;
27
use OCA\Deck\Db\BoardMapper;
28
use OCP\IConfig;
29
use OCP\IL10N;
30
use OCA\Deck\BadRequestException;
31
use OCP\PreConditionNotMetException;
32
33
class DefaultBoardService {
34
35
	private $boardMapper;
36
	private $boardService;
37
	private $stackService;
38
	private $cardService;
39
	private $config;
40
	private $l10n;
41
42
    public function __construct(
43
			IL10N $l10n,
44
			BoardMapper $boardMapper,
45
			BoardService $boardService,
46
			StackService $stackService,
47
			CardService $cardService,
48
			IConfig $config
49
			) {
50
51
		$this->boardService = $boardService;
52
		$this->stackService = $stackService;
53
		$this->cardService = $cardService;
54
		$this->config = $config;
55
		$this->boardMapper = $boardMapper;
56
		$this->l10n = $l10n;
57
    }
58
59
	/**
60
	 * Return true if this is the first time a user is acessing their instance with deck enabled
61
	 *
62
	 * @param $userId
63
	 * @return bool
64
	 */
65
	public function checkFirstRun($userId): bool {
66
		$firstRun = $this->config->getUserValue($userId, Application::APP_ID, 'firstRun', 'yes');
67
		$userBoards = $this->boardMapper->findAllByUser($userId);
68
69
		if ($firstRun === 'yes' && count($userBoards) === 0) {
70
			try {
71
				$this->config->setUserValue($userId, Application::APP_ID, 'firstRun', 'no');
72
			} catch (PreConditionNotMetException $e) {
73
				return false;
74
			}
75
			return true;
76
		}
77
78
		return false;
79
    }
80
81
	/**
82
	 * @param $title
83
	 * @param $userId
84
	 * @param $color
85
	 * @return \OCP\AppFramework\Db\Entity
86
	 * @throws \OCA\Deck\NoPermissionException
87
	 * @throws \OCA\Deck\StatusException
88
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
89
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
90
	 * @throws BadRequestException
91
	 */
92
	public function createDefaultBoard(string $title, string $userId, string $color) {
93
94
		if ($title === false || $title === null) {
95
			throw new BadRequestException('title must be provided');
96
		}
97
98
		if ($userId === false || $userId === null) {
99
			throw new BadRequestException('userId must be provided');
100
		}
101
102
		if ($color === false || $color === null) {
103
			throw new BadRequestException('color must be provided');
104
		}
105
106
        $defaultBoard = $this->boardService->create($title, $userId, $color);
107
        $defaultStacks = [];
108
        $defaultCards = [];
109
110
		$boardId = $defaultBoard->getId();
111
112
		$defaultStacks[] = $this->stackService->create($this->l10n->t('To do'), $boardId, 1);
113
		$defaultStacks[] = $this->stackService->create($this->l10n->t('Doing'), $boardId, 1);
114
		$defaultStacks[] = $this->stackService->create($this->l10n->t('Done'), $boardId, 1);
115
116
		$defaultCards[] = $this->cardService->create($this->l10n->t('Example Task 3'), $defaultStacks[0]->getId(), 'text', 0, $userId);
117
		$defaultCards[] = $this->cardService->create($this->l10n->t('Example Task 2'), $defaultStacks[1]->getId(), 'text', 0, $userId);
118
		$defaultCards[] = $this->cardService->create($this->l10n->t('Example Task 1'), $defaultStacks[2]->getId(), 'text', 0, $userId);
119
120
		return $defaultBoard;
121
    }
122
}
123