Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
88.89% covered (warning)
88.89%
8 / 9
CRAP
96.43% covered (success)
96.43%
27 / 28
Envelope
0.00% covered (danger)
0.00%
0 / 1
88.89% covered (warning)
88.89%
8 / 9
18
96.43% covered (success)
96.43%
27 / 28
 __construct
0.00% covered (danger)
0.00%
0 / 1
3.04
83.33% covered (warning)
83.33%
5 / 6
 wrap
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
2 / 2
 with
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 withoutAll
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 withoutStampsOfType
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
6 / 6
 last
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 all
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 getMessage
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 resolveAlias
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Messenger;
13
14use Symfony\Component\Messenger\Stamp\StampInterface;
15
16/**
17 * A message wrapped in an envelope with stamps (configurations, markers, ...).
18 *
19 * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
20 */
21final class Envelope
22{
23    private $stamps = [];
24    private $message;
25
26    /**
27     * @param object           $message
28     * @param StampInterface[] $stamps
29     */
30    public function __construct($message, array $stamps = [])
31    {
32        if (!\is_object($message)) {
33            throw new \TypeError(sprintf('Invalid argument provided to "%s()": expected object but got "%s".', __METHOD__, get_debug_type($message)));
34        }
35        $this->message = $message;
36
37        foreach ($stamps as $stamp) {
38            $this->stamps[\get_class($stamp)][] = $stamp;
39        }
40    }
41
42    /**
43     * Makes sure the message is in an Envelope and adds the given stamps.
44     *
45     * @param object|Envelope  $message
46     * @param StampInterface[] $stamps
47     */
48    public static function wrap($message, array $stamps = []): self
49    {
50        $envelope = $message instanceof self ? $message : new self($message);
51
52        return $envelope->with(...$stamps);
53    }
54
55    /**
56     * @return Envelope a new Envelope instance with additional stamp
57     */
58    public function with(StampInterface ...$stamps): self
59    {
60        $cloned = clone $this;
61
62        foreach ($stamps as $stamp) {
63            $cloned->stamps[\get_class($stamp)][] = $stamp;
64        }
65
66        return $cloned;
67    }
68
69    /**
70     * @return Envelope a new Envelope instance without any stamps of the given class
71     */
72    public function withoutAll(string $stampFqcn): self
73    {
74        $cloned = clone $this;
75
76        unset($cloned->stamps[$this->resolveAlias($stampFqcn)]);
77
78        return $cloned;
79    }
80
81    /**
82     * Removes all stamps that implement the given type.
83     */
84    public function withoutStampsOfType(string $type): self
85    {
86        $cloned = clone $this;
87        $type = $this->resolveAlias($type);
88
89        foreach ($cloned->stamps as $class => $stamps) {
90            if ($class === $type || is_subclass_of($class, $type)) {
91                unset($cloned->stamps[$class]);
92            }
93        }
94
95        return $cloned;
96    }
97
98    public function last(string $stampFqcn): ?StampInterface
99    {
100        return isset($this->stamps[$stampFqcn = $this->resolveAlias($stampFqcn)]) ? end($this->stamps[$stampFqcn]) : null;
101    }
102
103    /**
104     * @return StampInterface[]|StampInterface[][] The stamps for the specified FQCN, or all stamps by their class name
105     */
106    public function all(string $stampFqcn = null): array
107    {
108        if (null !== $stampFqcn) {
109            return $this->stamps[$this->resolveAlias($stampFqcn)] ?? [];
110        }
111
112        return $this->stamps;
113    }
114
115    /**
116     * @return object The original message contained in the envelope
117     */
118    public function getMessage(): object
119    {
120        return $this->message;
121    }
122
123    /**
124     * BC to be removed in 6.0.
125     */
126    private function resolveAlias(string $fqcn): string
127    {
128        static $resolved;
129
130        return $resolved[$fqcn] ?? ($resolved[$fqcn] = (new \ReflectionClass($fqcn))->getName());
131    }
132}