Completed
Push — master ( c78447...627e0e )
by Kamil
13s
created

NotificationChecker   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 80
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkCreationNotification() 0 6 1
A checkDeletionNotification() 0 6 1
A checkEditionNotification() 0 6 1
A checkSuccessNotificationMessage() 0 13 3
A humanizeResourceName() 0 4 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
namespace Sylius\Behat\Service;
13
14
use Sylius\Behat\Exception\NotificationExpectationMismatchException;
15
use Sylius\Behat\Service\Accessor\NotificationAccessorInterface;
16
use Webmozart\Assert\Assert;
17
18
/**
19
 * @author Łukasz Chruściel <[email protected]>
20
 */
21
final class NotificationChecker implements NotificationCheckerInterface
22
{
23
    /**
24
     * @var NotificationAccessorInterface
25
     */
26
    private $notificationAccessor;
27
28
    /**
29
     * @param NotificationAccessorInterface $notificationAccessor
30
     */
31
    public function __construct(NotificationAccessorInterface $notificationAccessor)
32
    {
33
        $this->notificationAccessor = $notificationAccessor;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     * 
39
     * @throws NotificationExpectationMismatchException
40
     */
41
    public function checkCreationNotification($resource)
42
    {
43
        $message = sprintf('Success %s has been successfully created.', $this->humanizeResourceName($resource));
44
45
        $this->checkSuccessNotificationMessage($message);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     * 
51
     * @throws NotificationExpectationMismatchException
52
     */
53
    public function checkDeletionNotification($resource)
54
    {
55
        $message = sprintf('Success %s has been successfully deleted.', $this->humanizeResourceName($resource));
56
57
        $this->checkSuccessNotificationMessage($message);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     * 
63
     * @throws NotificationExpectationMismatchException
64
     */
65
    public function checkEditionNotification($resource)
66
    {
67
        $message = sprintf('Success %s has been successfully updated.', $this->humanizeResourceName($resource));
68
69
        $this->checkSuccessNotificationMessage($message);
70
    }
71
72
    /**
73
     * @param string $message
74
     *
75
     * @throws NotificationExpectationMismatchException
76
     */
77
    public function checkSuccessNotificationMessage($message)
78
    {
79
        if ($this->notificationAccessor->hasSuccessMessage() && $this->notificationAccessor->hasMessage($message)) {
80
            return;
81
        }
82
83
        throw new NotificationExpectationMismatchException(
84
            'success',
85
            $message,
86
            $this->notificationAccessor->getMessageType(),
87
            $this->notificationAccessor->getMessage()
88
        );
89
    }
90
91
    /**
92
     * @param string $resourceName
93
     *
94
     * @return string
95
     */
96
    private function humanizeResourceName($resourceName)
97
    {
98
        return ucfirst(str_replace('_', ' ', $resourceName));
99
    }
100
}
101