Issues (19)

tests/Controller/DataTestController.php (4 issues)

1
<?php
2
3
namespace Dynamic\Foxy\Parser\Tests\Controller;
4
5
use Dynamic\Foxy\Extension\Purchasable;
6
use Dynamic\Foxy\Extension\Shippable;
7
use Dynamic\Foxy\Model\FoxyHelper;
8
use Dynamic\Foxy\Parser\Controller\FoxyController;
9
use GuzzleHttp\Client;
0 ignored issues
show
The type GuzzleHttp\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SilverStripe\CMS\Model\SiteTree;
11
use SilverStripe\Control\Controller;
12
use SilverStripe\Control\Director;
13
use SilverStripe\Core\Config\Config;
14
use SilverStripe\Core\Injector\Injector;
15
use SilverStripe\Dev\DebugView;
16
use SilverStripe\Dev\TestOnly;
17
use SilverStripe\ORM\ArrayList;
18
use SilverStripe\Security\Member;
19
use SilverStripe\Security\PasswordEncryptor;
20
use SilverStripe\View\ArrayData;
21
22
/**
23
 * Class DataTestController
24
 * @package Dynamic\Foxy\Parser\Controller
25
 */
26
class DataTestController extends Controller implements TestOnly
27
{
28
    /**
29
     * @var array
30
     */
31
    private static $allowed_actions = [
0 ignored issues
show
The private property $allowed_actions is not used, and could be removed.
Loading history...
32
        'index',
33
        'xml',
34
        'encryptedXML',
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    private static $data = [
0 ignored issues
show
The private property $data is not used, and could be removed.
Loading history...
41
        "TransactionDate" => "now",
42
        "OrderID" => "auto",
43
        "Email" => "auto",
44
        "Password" => "password",
45
        "OrderDetails" => [],
46
    ];
47
48
    /**
49
     * @throws \GuzzleHttp\Exception\GuzzleException
50
     * @throws \SilverStripe\ORM\ValidationException
51
     * @throws \SilverStripe\Security\PasswordEncryptor_NotFoundException
52
     */
53
    public function index()
54
    {
55
        $rules = Director::config()->get('rules');
56
        $rule = array_search(FoxyController::class, $rules);
57
        $myURL = Director::absoluteBaseURL() . explode('//', $rule)[0];
58
59
        $helper = new FoxyHelper();
60
        $myKey = $helper->config()->get('secret');
0 ignored issues
show
The assignment to $myKey is dead and can be removed.
Loading history...
61
62
        $XMLOutput_encrypted = $this->encryptedXML();
63
        $XMLOutput_encrypted = urlencode($XMLOutput_encrypted);
64
65
        $client = new Client();
66
        $response = $client->request('POST', $myURL, [
67
            'form_params' => ['FoxyData' => $XMLOutput_encrypted],
68
        ]);
69
70
        $configString = print_r(static::config()->get('data'), true);
71
        /** @var DebugView $view */
72
        $view = Injector::inst()->create(DebugView::class);
73
        echo $view->renderHeader();
74
        echo '<div class="info">';
75
        echo "<h2>Config:</h2><pre>$configString</pre>";
76
        if ($this->getRequest()->getVar('data')) {
77
            //echo "<h2>Data:</h2><pre>{$xml->HTML()}</pre>";
78
        }
79
        echo "<h2>Response:</h2><pre>" . $response->getBody() . "</pre>";
80
        echo '<p></p>';
81
        echo '</div>';
82
        echo $view->renderFooter();
83
    }
84
85
    /**
86
     * @throws \SilverStripe\Security\PasswordEncryptor_NotFoundException
87
     */
88
    private function updateConfig()
89
    {
90
        $data = Config::inst()->get(self::class, 'data');
91
        $transactionDate = $data['TransactionDate'];
92
        static::config()->merge('data', [
93
            'TransactionDate' => strtotime($transactionDate),
94
        ]);
95
96
        $orderID = $data['OrderID'];
97
        if ($orderID === 'auto' || $orderID < 1) {
98
            static::config()->merge('data', [
99
                'OrderID' => rand(0, 5000),
100
            ]);
101
        }
102
103
        $email = $data['Email'];
104
        if ($email === 'auto') {
105
            static::config()->merge('data', [
106
                'Email' => $this->generateEmail(),
107
            ]);
108
        }
109
110
        $orderDetails = $data['OrderDetails'];
111
        if ($orderDetails === null || count($orderDetails) === 0) {
112
            static::config()->merge('data', [
113
                'OrderDetails' => [
114
                    $this->generateOrderDetail(),
115
                ],
116
            ]);
117
        }
118
119
        if (!array_key_exists('Salt', $data)) {
120
            static::config()->merge('data', [
121
                'Salt' => 'faGgWXUTdZ7i42lpA6cljzKeGBeUwShBSNHECwsJmt',
122
            ]);
123
        }
124
125
        if (!array_key_exists('HashType', $data)) {
126
            static::config()->merge('data', [
127
                'HashType' => 'sha1_v2.4',
128
            ]);
129
        }
130
131
        $data = static::config()->get('data');
132
        if (!array_key_exists('HashedPassword', $data)) {
133
            $encryptor = PasswordEncryptor::create_for_algorithm($data['HashType']);
134
            static::config()->merge('data', [
135
                'HashedPassword' => $encryptor->encrypt($data['Password'], $data['Salt']),
136
            ]);
137
        }
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    private function generateEmail()
144
    {
145
        $emails = Member::get()->filter([
146
            'Email:EndsWith' => '@example.com',
147
        ])->column('Email');
148
149
        if ($emails && count($emails)) {
150
            $email = $emails[count($emails) - 1];
151
152
            return preg_replace_callback(
153
                "|(\d+)|",
154
                function ($mathces) {
155
                    return ++$mathces[1];
156
                },
157
                $email
158
            );
159
        }
160
161
        return '[email protected]';
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    private function generateOrderDetail()
168
    {
169
        return [
170
            'Title' => 'foo',
171
            'Price' => 20.00,
172
            'Quantity' => 1,
173
            'Weight' => 0.1,
174
            'DeliveryType' => 'shipped',
175
            'CategoryDescription' => 'Default cateogry',
176
            'CategoryCode' => 'DEFAULT',
177
            'Options' => ArrayList::create([
178
                [
179
                    'Name' => 'color',
180
                    'OptionValue' => 'blue',
181
                    'PriceMod' => '',
182
                    'WeightMod' => '',
183
                ],
184
                [
185
                    'Name' => 'product_id',
186
                    'OptionValue' => $this->getTestProduct(),
187
                    'PriceMod' => '',
188
                    'WeightMod' => '',
189
                ],
190
            ]),
191
        ];
192
    }
193
194
    /**
195
     * @return bool
196
     */
197
    public function getTestProduct()
198
    {
199
        $pages = SiteTree::get();
200
        foreach ($pages as $page) {
201
            if ($page->hasExtension(Purchasable::class) || $page->hasExtension(Shippable::class)) {
202
                return $page->ID;
203
            }
204
        }
205
206
        return false;
207
    }
208
209
    /**
210
     * @return mixed|string
211
     * @throws \SilverStripe\Security\PasswordEncryptor_NotFoundException
212
     */
213
    public function xml()
214
    {
215
        if ($this->config()->get('run_config_update')) {
216
            $this->updateConfig();
217
        }
218
219
        $config = static::config()->get('data');
220
221
        if (isset($config['OrderDetails']) && !$config['OrderDetails'] instanceof ArrayData) {
222
            $config['OrderDetails'] = ArrayList::create($config['OrderDetails']);
223
        }
224
225
        $xml = $this->renderWith('TestData', $config);
226
227
        return $xml->RAW();
228
    }
229
230
    /**
231
     * @return string
232
     * @throws \SilverStripe\ORM\ValidationException
233
     * @throws \SilverStripe\Security\PasswordEncryptor_NotFoundException
234
     */
235
    public function encryptedXML()
236
    {
237
        $helper = new FoxyHelper();
238
239
        return \rc4crypt::encrypt($helper->config()->get('secret'), $this->xml());
240
    }
241
}
242