Source of file DataTestController.php
Size: 6,953 Bytes - Last Modified: 2021-12-24T06:46:12+00:00
/var/www/docs.ssmods.com/process/src/tests/Controller/DataTestController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 | <?php namespace Dynamic\Foxy\Parser\Tests\Controller; use Dynamic\Foxy\Extension\Purchasable; use Dynamic\Foxy\Extension\Shippable; use Dynamic\Foxy\Model\FoxyHelper; use Dynamic\Foxy\Parser\Controller\FoxyController; use GuzzleHttp\Client; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\DebugView; use SilverStripe\Dev\TestOnly; use SilverStripe\ORM\ArrayList; use SilverStripe\Security\Member; use SilverStripe\Security\PasswordEncryptor; use SilverStripe\View\ArrayData; /** * Class DataTestController * @package Dynamic\Foxy\Parser\Controller */ class DataTestController extends Controller implements TestOnly { /** * @var array */ private static $allowed_actions = [ 'index', 'xml', 'encryptedXML', ]; /** * @var array */ private static $data = [ "TransactionDate" => "now", "OrderID" => "auto", "Email" => "auto", "Password" => "password", "OrderDetails" => [], ]; /** * @throws \GuzzleHttp\Exception\GuzzleException * @throws \SilverStripe\ORM\ValidationException * @throws \SilverStripe\Security\PasswordEncryptor_NotFoundException */ public function index() { $rules = Director::config()->get('rules'); $rule = array_search(FoxyController::class, $rules); $myURL = Director::absoluteBaseURL() . explode('//', $rule)[0]; $helper = new FoxyHelper(); $myKey = $helper->config()->get('secret'); $XMLOutput_encrypted = $this->encryptedXML(); $XMLOutput_encrypted = urlencode($XMLOutput_encrypted); $client = new Client(); $response = $client->request('POST', $myURL, [ 'form_params' => ['FoxyData' => $XMLOutput_encrypted], ]); $configString = print_r(static::config()->get('data'), true); /** @var DebugView $view */ $view = Injector::inst()->create(DebugView::class); echo $view->renderHeader(); echo '<div class="info">'; echo "<h2>Config:</h2><pre>$configString</pre>"; if ($this->getRequest()->getVar('data')) { //echo "<h2>Data:</h2><pre>{$xml->HTML()}</pre>"; } echo "<h2>Response:</h2><pre>" . $response->getBody() . "</pre>"; echo '<p></p>'; echo '</div>'; echo $view->renderFooter(); } /** * @throws \SilverStripe\Security\PasswordEncryptor_NotFoundException */ private function updateConfig() { $data = Config::inst()->get(self::class, 'data'); $transactionDate = $data['TransactionDate']; static::config()->merge('data', [ 'TransactionDate' => strtotime($transactionDate), ]); $orderID = $data['OrderID']; if ($orderID === 'auto' || $orderID < 1) { static::config()->merge('data', [ 'OrderID' => rand(0, 5000), ]); } $email = $data['Email']; if ($email === 'auto') { static::config()->merge('data', [ 'Email' => $this->generateEmail(), ]); } $orderDetails = $data['OrderDetails']; if ($orderDetails === null || count($orderDetails) === 0) { static::config()->merge('data', [ 'OrderDetails' => [ $this->generateOrderDetail(), ], ]); } if (!array_key_exists('Salt', $data)) { static::config()->merge('data', [ 'Salt' => 'faGgWXUTdZ7i42lpA6cljzKeGBeUwShBSNHECwsJmt', ]); } if (!array_key_exists('HashType', $data)) { static::config()->merge('data', [ 'HashType' => 'sha1_v2.4', ]); } $data = static::config()->get('data'); if (!array_key_exists('HashedPassword', $data)) { $encryptor = PasswordEncryptor::create_for_algorithm($data['HashType']); static::config()->merge('data', [ 'HashedPassword' => $encryptor->encrypt($data['Password'], $data['Salt']), ]); } } /** * @return string */ private function generateEmail() { $emails = Member::get()->filter([ 'Email:EndsWith' => '@example.com', ])->column('Email'); if ($emails && count($emails)) { $email = $emails[count($emails) - 1]; return preg_replace_callback( "|(\d+)|", function ($mathces) { return ++$mathces[1]; }, ); } return 'example0@example.com'; } /** * @return array */ private function generateOrderDetail() { return [ 'Title' => 'foo', 'Price' => 20.00, 'Quantity' => 1, 'Weight' => 0.1, 'DeliveryType' => 'shipped', 'CategoryDescription' => 'Default cateogry', 'CategoryCode' => 'DEFAULT', 'Options' => ArrayList::create([ [ 'Name' => 'color', 'OptionValue' => 'blue', 'PriceMod' => '', 'WeightMod' => '', ], [ 'Name' => 'product_id', 'OptionValue' => $this->getTestProduct(), 'PriceMod' => '', 'WeightMod' => '', ], ]), ]; } /** * @return bool */ public function getTestProduct() { $pages = SiteTree::get(); foreach ($pages as $page) { if ($page->hasExtension(Purchasable::class) || $page->hasExtension(Shippable::class)) { return $page->ID; } } return false; } /** * @return mixed|string * @throws \SilverStripe\Security\PasswordEncryptor_NotFoundException */ public function xml() { if ($this->config()->get('run_config_update')) { $this->updateConfig(); } $config = static::config()->get('data'); if (isset($config['OrderDetails']) && !$config['OrderDetails'] instanceof ArrayData) { $config['OrderDetails'] = ArrayList::create($config['OrderDetails']); } $xml = $this->renderWith('TestData', $config); return $xml->RAW(); } /** * @return string * @throws \SilverStripe\ORM\ValidationException * @throws \SilverStripe\Security\PasswordEncryptor_NotFoundException */ public function encryptedXML() { $helper = new FoxyHelper(); return \rc4crypt::encrypt($helper->config()->get('secret'), $this->xml()); } } |