-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strategy.php
61 lines (54 loc) · 2.06 KB
/
Strategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* @author Artemii Karkusha
* @copyright Copyright (c) (https://www.linkedin.com/in/artemiy-karkusha/)
*/
declare(strict_types=1);
namespace ArtemiiKarkusha\DesignPatterns\Controller\Test;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultFactory;
use ArtemiiKarkusha\DesignPatterns\Api\Strategy\SorterListInterface;
use ArtemiiKarkusha\DesignPatterns\Service\Strategy\FastSortStrategy;
use Magento\Framework\Controller\ResultInterface;
class Strategy implements HttpGetActionInterface
{
/**
* @param ResultFactory $resultFactory
* @param SorterListInterface $sorterList
* @param FastSortStrategy $fastSortStrategy
*/
public function __construct(
readonly private ResultFactory $resultFactory,
readonly private SorterListInterface $sorterList,
readonly private FastSortStrategy $fastSortStrategy
) {
}
/**
* @inheritDoc
*/
public function execute(): ResultInterface
{
return $this->resultFactory->create(ResultFactory::TYPE_RAW)->setContents($this->getContents());
}
/**
* @return string
*/
public function getContents(): string
{
$list = range(0, 2000);
$timeStart = microtime(true);
$sortedBubbleArray = $this->sorterList->sort($list);
$timeEnd = microtime(true);
$executionTime = ($timeEnd - $timeStart);
echo '<b>Total Execution Time sortedSmallArray:</b> '.$executionTime.' Sec <br>';
$timeStart = microtime(true);
$sortedBigArray = $this->sorterList->setSortStrategy($this->fastSortStrategy)->sort($list);
$timeEnd = microtime(true);
$executionTime = ($timeEnd - $timeStart);
echo '<b>Total Execution Time sortedSmallArray:</b> '.$executionTime.' Sec <br>';
$resultContent = '<div><b>Strategy</b></div>';
$resultContent .= '<pre>Sorted Small Array: ' . count($sortedBubbleArray) . '</pre>';
$resultContent .= '<pre>Sorted Big Array: ' . count($sortedBigArray) . '</pre>';
return $resultContent;
}
}