Completed
Push — 1.5 ( 17c098...006ba9 )
by Kamil
30:59
created

NotificationChecker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkNotification() 0 13 4
A resolveClass() 0 12 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Behat\Service;
15
16
use Sylius\Behat\Exception\NotificationExpectationMismatchException;
17
use Sylius\Behat\NotificationType;
18
use Sylius\Behat\Service\Accessor\NotificationAccessorInterface;
19
use Webmozart\Assert\Assert;
20
21
final class NotificationChecker implements NotificationCheckerInterface
22
{
23
    /** @var NotificationAccessorInterface */
24
    private $notificationAccessor;
25
26
    public function __construct(NotificationAccessorInterface $notificationAccessor)
27
    {
28
        $this->notificationAccessor = $notificationAccessor;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function checkNotification(string $message, NotificationType $type): void
35
    {
36
        foreach ($this->notificationAccessor->getMessageElements() as $messageElement) {
37
            if (
38
                false !== strpos($messageElement->getText(), $message) &&
39
                $messageElement->hasClass($this->resolveClass($type))
40
            ) {
41
                return;
42
            }
43
        }
44
45
        throw new NotificationExpectationMismatchException($type, $message);
46
    }
47
48
    private function resolveClass(NotificationType $type): string
49
    {
50
        $typeClassMap = [
51
            'failure' => 'negative',
52
            'info' => 'info',
53
            'success' => 'positive',
54
        ];
55
56
        Assert::keyExists($typeClassMap, $type->__toString());
57
58
        return $typeClassMap[$type->__toString()];
59
    }
60
}
61