TableFactory::getModel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * CakePHP permission handling library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\CakePermission;
7
8
use Cake\Core\Configure;
9
use Cake\ORM\TableRegistry;
10
use Slince\CakePermission\Model\Table\UsersTable;
11
use Slince\CakePermission\Model\Table\RolesTable;
12
use Slince\CakePermission\Model\Table\PermissionsTable;
13
use Cake\ORM\Table;
14
15
class TableFactory
16
{
17
    /**
18
     * Array of default models classes
19
     * @var array
20
     */
21
    protected static $defaultModelClasses = [
22
        'Users' => UsersTable::class,
23
        'Roles' => RolesTable::class,
24
        'Permissions' => PermissionsTable::class
25
    ];
26
27
    /**
28
     * Gets the role model
29
     * @return Table
30
     */
31
    public static function getRoleModel()
32
    {
33
        return static::getModel('Roles');
34
    }
35
36
    /**
37
     * Gets the permission model
38
     * @return Table
39
     */
40
    public static function getPermissionModel()
41
    {
42
        return static::getModel('Permissions');
43
    }
44
45
    /**
46
     * Gets the user model
47
     * @return Table
48
     */
49
    public static function getUserModel()
50
    {
51
        return static::getModel('Users');
52
    }
53
54
    /**
55
     * Gets the model instance
56
     * @param $name
57
     * @return Table
58
     */
59
    public static function getModel($name)
60
    {
61
        return TableRegistry::get("_Permission{$name}", [
62
            'className' => static::getModelClass($name)
63
        ]);
64
    }
65
66
    /**
67
     * Gets the default model class
68
     * @param string $name
69
     * @return string
70
     */
71
    public static function getModelClass($name)
72
    {
73
        return Configure::read("Permission.tableClassMap.{$name}") ?: static::$defaultModelClasses[$name];
74
    }
75
}