TranslatorAwareTrait::setTranslator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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