Completed
Branch master (fafb06)
by Nate
04:30
created

Domains::normalizeCategory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/domains/license
6
 * @link       https://www.flipboxfactory.com/software/domains/
7
 */
8
9
namespace flipbox\domains;
10
11
use Craft;
12
use craft\base\Plugin as BasePlugin;
13
use craft\events\RegisterComponentTypesEvent;
14
use craft\services\Fields;
15
use flipbox\domains\fields\Domains as DomainsField;
16
use yii\base\Event;
17
use yii\log\Logger;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since  1.0.0
22
 */
23
class Domains extends BasePlugin
24
{
25
    /**
26
     * @inheritdoc
27
     */
28
    public function init()
29
    {
30
        // Do parent
31
        parent::init();
32
33
        // Register our fields
34
        Event::on(
35
            Fields::class,
36
            Fields::EVENT_REGISTER_FIELD_TYPES,
37
            function (RegisterComponentTypesEvent $event) {
38
                $event->types[] = DomainsField::class;
39
            }
40
        );
41
    }
42
43
    /**
44
     * @return services\Field
45
     */
46
    public function getField()
47
    {
48
        return $this->get('field');
49
    }
50
51
    /**
52
     * @return services\Domains
53
     */
54
    public function getDomains()
55
    {
56
        return $this->get('domains');
57
    }
58
59
    /**
60
     * @return services\DomainAssociations
61
     */
62
    public function getDomainAssociations()
63
    {
64
        return $this->get('domainAssociations');
65
    }
66
67
    /**
68
     * @return services\Relationship
69
     */
70
    public function getRelationship()
71
    {
72
        return $this->get('relationship');
73
    }
74
75
    /*******************************************
76
     * LOGGING
77
     *******************************************/
78
79
    /**
80
     * Logs a trace message.
81
     * Trace messages are logged mainly for development purpose to see
82
     * the execution work flow of some code.
83
     * @param string $message the message to be logged.
84
     * @param string $category the category of the message.
85
     */
86
    public static function trace($message, string $category = null)
87
    {
88
        Craft::getLogger()->log($message, Logger::LEVEL_TRACE, self::normalizeCategory($category));
89
    }
90
91
    /**
92
     * Logs an error message.
93
     * An error message is typically logged when an unrecoverable error occurs
94
     * during the execution of an application.
95
     * @param string $message the message to be logged.
96
     * @param string $category the category of the message.
97
     */
98
    public static function error($message, string $category = null)
99
    {
100
        Craft::getLogger()->log($message, Logger::LEVEL_ERROR, self::normalizeCategory($category));
101
    }
102
103
    /**
104
     * Logs a warning message.
105
     * A warning message is typically logged when an error occurs while the execution
106
     * can still continue.
107
     * @param string $message the message to be logged.
108
     * @param string $category the category of the message.
109
     */
110
    public static function warning($message, string $category = null)
111
    {
112
        Craft::getLogger()->log($message, Logger::LEVEL_WARNING, self::normalizeCategory($category));
113
    }
114
115
    /**
116
     * Logs an informative message.
117
     * An informative message is typically logged by an application to keep record of
118
     * something important (e.g. an administrator logs in).
119
     * @param string $message the message to be logged.
120
     * @param string $category the category of the message.
121
     */
122
    public static function info($message, string $category = null)
123
    {
124
        Craft::getLogger()->log($message, Logger::LEVEL_INFO, self::normalizeCategory($category));
125
    }
126
127
    /**
128
     * @param string|null $category
129
     * @return string
130
     */
131
    private static function normalizeCategory(string $category = null)
132
    {
133
        $normalizedCategory = 'Domains';
134
135
        if ($category === null) {
136
            return $normalizedCategory;
137
        }
138
139
        return $normalizedCategory . ': ' . $category;
140
    }
141
}
142