Completed
Push — feature/refactor-app-design ( 84f9ff...b18d21 )
by Avtandil
04:09
created

Config::addCommandsPath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 10
nc 4
nop 2
crap 4
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot;
12
13
use Illuminate\Config\Repository;
14
15
class Config extends Repository
16
{
17
    /**
18
     * Add a single custom commands path
19
     *
20
     * @param string $path Custom commands path to add
21
     * @param bool $before If the path should be prepended or appended to the list
22
     *
23
     * @return void
24
     */
25 36
    public function addCommandsPath($path, $before = true)
26
    {
27 36
        $paths = $this->get('commands.paths', []);
28
29 36
        if (! is_dir($path)) {
30 1
            TelegramLog::error('Commands path "%s" does not exist.', $path);
31 36
        } else if (! in_array($path, $paths, true)) {
32 36
            if ($before) {
33 35
                array_unshift($paths, $path);
34
            } else {
35 1
                $paths[] = $path;
36
            }
37
        }
38
39 36
        $this->set('commands.paths', $paths);
40 36
    }
41
42
    /**
43
     * Add multiple custom commands paths
44
     *
45
     * @param array $paths Custom commands paths to add
46
     * @param bool $before If the paths should be prepended or appended to the list
47
     *
48
     * @return void
49
     */
50 3
    public function addCommandsPaths(array $paths, $before = true)
51
    {
52 3
        foreach ($paths as $path) {
53 3
            $this->addCommandsPath($path, $before);
54
        }
55 3
    }
56
57
    /**
58
     * Return the list of commands paths
59
     *
60
     * @return array
61
     */
62 6
    public function getCommandsPaths()
63
    {
64 6
        return $this->get('commands.paths', []);
65
    }
66
67
    /**
68
     * Enable a single Admin account
69
     *
70
     * @param integer $admin_id Single admin id
71
     *
72
     * @return void
73
     */
74 2
    public function addAdmin($admin_id)
75
    {
76 2
        $admins = $this->get('admins', []);
77
78 2
        if (! is_int($admin_id) || $admin_id <= 0) {
79 1
            TelegramLog::error('Invalid value "%s" for admin.', $admin_id);
80 2
        } else if (! in_array($admin_id, $admins, true)) {
81 2
            $admins[] = $admin_id;
82
        }
83
84 2
        $this->set('admins', $admins);
85 2
    }
86
87
    /**
88
     * Enable a list of Admin Accounts
89
     *
90
     * @param array $admin_ids List of admin ids
91
     *
92
     * @return void
93
     */
94 2
    public function addAdmins(array $admin_ids)
95
    {
96 2
        foreach ($admin_ids as $admin_id) {
97 2
            $this->addAdmin($admin_id);
98
        }
99 2
    }
100
101
    /**
102
     * Get list of admins
103
     *
104
     * @return array
105
     */
106 5
    public function getAdmins()
107
    {
108 5
        return $this->get('admins', []);
109
    }
110
}
111