TranslatorAwareTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setTranslator() 0 3 1
A translator() 0 8 2
1
<?php
2
3
namespace Charcoal\Translator;
4
5
use RuntimeException;
6
7
// From 'charcoal-translator'
8
use Charcoal\Translator\Translator;
9
10
/**
11
 * The Translator Aware Trait provides the methods necessary for an object
12
 * to use a "Translator" service.
13
 */
14
trait TranslatorAwareTrait
15
{
16
    /**
17
     * The Translator service.
18
     *
19
     * @var Translator
20
     */
21
    private $translator;
22
23
    /**
24
     * Set the translator service.
25
     *
26
     * @param  Translator $translator The Translator service.
27
     * @return void
28
     */
29
    protected function setTranslator(Translator $translator)
30
    {
31
        $this->translator = $translator;
32
    }
33
34
    /**
35
     * Retrieve the translator service.
36
     *
37
     * @throws RuntimeException If the translator is accessed before having been set.
38
     * @return Translator
39
     */
40
    protected function translator()
41
    {
42
        if ($this->translator === null) {
43
            throw new RuntimeException(
44
                'Translator has not been set on this object.'
45
            );
46
        }
47
        return $this->translator;
48
    }
49
}
50