AggregateHydratorMockTrait::hydrate()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 8
nop 1
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * apparat-resource
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Resource
8
 * @subpackage  Apparat\Resource\Tests
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation Fixture (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Resource\Tests;
38
39
use Apparat\Resource\Domain\Model\Part\AbstractPartAggregate;
40
use Apparat\Resource\Domain\Model\Part\PartAggregateInterface;
41
42
/**
43
 * Mock methods for multipart hydrators
44
 *
45
 * @package     Apparat\Resource
46
 * @subpackage  Apparat\Resource\Tests
47
 * @property string $aggregateClass Part aggregate class name
48
 */
49
trait AggregateHydratorMockTrait
50
{
51
    /**
52
     * Validate the parameters accepted by this hydrator
53
     *
54
     * By default, a multipart parameter accepts exactly two parameters:
55
     * - the minimum number of occurrences of the contained part aggregate
56
     * - the maximum number of occurrences of the contained part aggregate
57
     *
58
     * @param array $parameters Parameters
59
     * @return boolean Parameters are valid
60
     */
61
    public static function validateParameters(...$parameters)
62
    {
63
64
        // If the default validation should be used
65
        if (getenv('MOCK_VALIDATE_PARAMETERS') != 1) {
66
            /** @noinspection PhpUndefinedMethodInspection */
67
            return parent::validateParameters(...$parameters);
68
69
            // Else return a mock result
70
        } else {
71
            return false;
72
        }
73
    }
74
75
    /**
76
     * Translate data to a file part
77
     *
78
     * @param string $data Part data
79
     * @return PartAggregateInterface Resource part
80
     */
81
    public function hydrate($data)
82
    {
83
        if (getenv('MOCK_AGGREGATE_CLASS') == 1) {
84
            $this->aggregateClass = self::class;
85
        }
86
87
        /** @var AbstractPartAggregate $aggregate */
88
        /** @noinspection  PhpUndefinedMethodInspection */
89
        $aggregate = parent::hydrate(null);
90
        foreach (explode('|', $data) as $part => $str) {
91
            if (getenv('MOCK_OCCURRENCE_NUMBER') == 1) {
92
                $aggregate->assign(0, $str, $part);
93
            } elseif (getenv('MOCK_ASSIGNMENT_PART_IDENTIFIER') == 1) {
94
                $aggregate->assign("_$part", $str, 0);
95
            } else {
96
                $aggregate->assign("$part", $str);
97
            }
98
        }
99
        return $aggregate;
100
    }
101
102
    /**
103
     * Dehydrate a single occurrence
104
     *
105
     * @param array $occurrence Occurrence
106
     * @return string Dehydrated occurrence
107
     */
108
    protected function dehydrateOccurrence(array $occurrence)
109
    {
110
        // If the default validation should be used
111
        if (getenv('MOCK_OCCURRENCE_DEHYDRATION') != 1) {
112
            // If an empty occurrence shall be tested
113
            if (getenv('MOCK_EMPTY_OCCURRENCE') == 1) {
114
                /** @noinspection PhpUndefinedMethodInspection */
115
                return parent::dehydrateOccurrence([]);
116
117
                // If an invalid subhydrator name should be tested
118
            } elseif (getenv('MOCK_SUBHYDRATOR_NAME') == 1) {
119
                /** @noinspection PhpUndefinedMethodInspection */
120
                return parent::dehydrateOccurrence(
121
                    array_combine(
122
                        array_map(
123
                            function ($name) {
124
                                return '_'.$name.'_';
125
                            },
126
                            array_keys($occurrence)
127
                        ),
128
                        array_values($occurrence)
129
                    )
130
                );
131
132
                // If an invalid part instance should be tested
133
            } elseif (getenv('MOCK_PART_INSTANCE') == 1) {
134
                /** @noinspection PhpUndefinedMethodInspection */
135
                return parent::dehydrateOccurrence(array_fill_keys(array_keys($occurrence), null));
136
137
                // Else: Regular processing
138
            } else {
139
                /** @noinspection PhpUndefinedMethodInspection */
140
                return parent::dehydrateOccurrence($occurrence);
141
            }
142
            // Else return a mock result
143
        } else {
144
            return [];
145
        }
146
    }
147
}
148