summaryrefslogtreecommitdiff
path: root/src/NeonXP/DozorApi.php
blob: 8c7c510849acce91b4bdbe347f0ead9570a3727b (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
 * @author Alexander "NeonXP" Kiryukhin
 */
namespace NeonXP;
use Doctrine\Common\Cache\CacheProvider;
use NeonXP\TelegramApi\Types\Chat;
use Symfony\Component\CssSelector\Node\AbstractNode;
use Symfony\Component\DomCrawler\Crawler;


/**
 * Class DozorApi
 * @package NeonXP
 */
class DozorApi
{
    /**
     * @var CacheProvider
     */
    private $cache;

    /**
     * DozorApi constructor.
     * @param CacheProvider $cache
     */
    public function __construct(CacheProvider $cache)
    {
        $this->cache = $cache;
    }

    public function sendCode(Chat $chat, $code)
    {
        $params = [
            "log" => 'on',
            "mes" => '',
            "legend" => 'on',
            "nostat" => '',
            "notext" => '',
            "refresh" => '0',
            "bonus" => '',
            "kladMap" => '',
            "notags" => '',
            "cod" => $code,
            "action" => 'entcod',
        ];
        $data = http_build_query($params);
        $contextData = [
            'http' => [
                'method' => 'POST',
                'header'  => 'Content-type: application/x-www-form-urlencoded',
                'content' => $data
            ]
        ];
        $context = stream_context_create($contextData);
        $result = file_get_contents($this->getLink($chat), false, $context);
        $contextData['result'] = $result;

        return json_encode($contextData, JSON_UNESCAPED_UNICODE);
    }

    private function parseResult($html)
    {
        $crawler = new Crawler($html);

        $answer = [
            $this->parseCodeEnter($html),
            $this->parseTask($crawler)
        ];

        $answer = array_merge($answer,  $this->parseCodes($crawler));
        $answer = array_filter($answer);

        return implode(PHP_EOL, $answer);
    }

    private function parseCodeEnter($html)
    {
        $result = iconv('cp1251', 'utf8', $html);
        $result = mb_strtolower($result, 'utf8');
        if (strpos($result, 'код принят')) {
            $result = 'Код принят';
        } else {
            $result = 'Код НЕ принят';
        }

        return $result;
    }

    private function parseTask(Crawler $crawler)
    {
        return $crawler->filter('body > p')->reduce(function ($node) {
            return strpos($node->text(), 'Задание ');
        })->text();
    }

    private function parseCodes(Crawler $crawler)
    {
        $codesHtml = $crawler->filter('body > .dcodes')->html();
        $codesHtml = preg_replace('/\<span\ style\=\"color\:red\"\>(.+?)\<\/span\>/i', '*[$1]*', $codesHtml);
        $codesHtml = preg_replace('/\<br(\s*)?\/?\>/i', PHP_EOL, $codesHtml);
        $codesHtml = strip_tags($codesHtml);
        $codesHtml = explode(PHP_EOL, $codesHtml);
        array_shift($codesHtml);

        return $codesHtml;
    }

    private function getLink(Chat $chat)
    {
        return $this->cache->fetch("link_{$chat->getId()}");
    }
}