TranslationHistorySubscriber   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 87.5%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 6
c 2
b 1
f 1
lcom 1
cbo 4
dl 0
loc 65
ccs 21
cts 24
cp 0.875
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B updateHistory() 0 27 3
A __construct() 0 12 3
1
<?php
2
/*
3
 * This file is part of the AsmTranslationLoaderBundle package.
4
 *
5
 * (c) Marc Aschmann <[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 Asm\TranslationLoaderBundle\EventListener;
12
13
use Asm\TranslationLoaderBundle\Event\TranslationEvent;
14
use Asm\TranslationLoaderBundle\Model\TranslationHistoryInterface;
15
use Asm\TranslationLoaderBundle\Model\TranslationHistoryManagerInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
use Symfony\Component\Security\Core\SecurityContextInterface;
18
19
/**
20
 * Class TranslationHistorySubscriber
21
 *
22
 * @author marc aschmann <[email protected]>
23
 * @author Christian Flothmann <[email protected]>
24
 */
25
class TranslationHistorySubscriber
26
{
27
    /**
28
     * @var TranslationHistoryManagerInterface
29
     */
30
    private $translationHistoryManager;
31
32
    /**
33
     * @var SecurityContextInterface|TokenStorageInterface
34
     */
35
    private $tokenStorage;
36
37
    /**
38
     * @param TranslationHistoryManagerInterface             $translationHistoryManager
39
     * @param SecurityContextInterface|TokenStorageInterface $tokenStorage
40
     */
41 6
    public function __construct(
42
        TranslationHistoryManagerInterface $translationHistoryManager,
43
        $tokenStorage
44
    ) {
45 6
        $this->translationHistoryManager = $translationHistoryManager;
46
47 6
        if (!$tokenStorage instanceof SecurityContextInterface && !$tokenStorage instanceof TokenStorageInterface) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Securi...ecurityContextInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
48
            throw new \InvalidArgumentException('Token storage must be an instance of Symfony\Component\Security\Core\SecurityContextInterface or Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface.');
49
        }
50
51 6
        $this->tokenStorage = $tokenStorage;
52 6
    }
53
54
    /**
55
     * Event-based update of the translation history.
56
     *
57
     * @param TranslationEvent $event     The event triggering the update
58
     * @param string           $eventName The name of the triggered event
59
     *
60
     * @return TranslationHistoryInterface The new translation history entry
61
     */
62 6
    public function updateHistory(TranslationEvent $event, $eventName = null)
63
    {
64 6
        $translation = $event->getTranslation();
65
66 6
        $historyEntry = $this->translationHistoryManager->createTranslationHistory();
67 6
        $historyEntry->setDateOfChange(new \DateTime());
68 6
        $historyEntry->setMessageDomain($translation->getMessageDomain());
69 6
        $historyEntry->setTransKey($translation->getTransKey());
70 6
        $historyEntry->setTransLocale($translation->getTransLocale());
71 6
        $historyEntry->setTranslation($translation->getTranslation());
72
73 6
        if (null === $eventName) {
74
            $eventName = $event->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<Asm\TranslationLo...Event\TranslationEvent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
        }
76
77 6
        $historyEntry->setUserAction(strtolower(substr($eventName, 4)));
78
79 6
        if (null !== $token = $this->tokenStorage->getToken()) {
80 3
            $historyEntry->setUserName($token->getUsername());
81 3
        } else {
82 3
            $historyEntry->setUserName('anonymous');
83
        }
84
85 6
        $this->translationHistoryManager->updateTranslationHistory($historyEntry);
86
87 6
        return $historyEntry;
88
    }
89
}
90