sveden-parser/src/Manager.php

71 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace SvedenParser;
use SvedenParser\Http\HttpClient;
use SvedenParser\Logger\HtmlLogger;
abstract class Manager
{
protected Service $service;
protected Repository $repository;
protected HttpClient $httpClient;
protected HtmlLogger $htmlLogger;
public function __construct()
{
$this->httpClient = new HttpClient();
$this->htmlLogger = new HtmlLogger(SVEDEN_PARSER . '/log/html.log');
}
abstract function collectData(array $site): void;
/**
* Получить массив сайтов
* @param array $params Массив сайтов, у которых нужны обновиленные URL
* @return array
*/
public function getSites(array $params = []): array
{
if (!$params) {
return $this->repository->getSitesFromNiimko();
} else {
return $this->repository->getSitesFromMiccedu($params);
}
}
/**
* Условие выхода
* @param array $site
* @return bool
*/
protected function isExit(array $site): bool
{
// Нет URL сайта вуза
if (!$site['site']) {
return true;
}
// Уже в базе
if (in_array($site['org_id'], $this->repository->universities())) {
// return true;
}
return false;
}
public function getSitesFromLog(string $path): array
{
try {
$result = [];
$data = file($path);
foreach ($data as &$dt) {
$dt = explode(' ', $dt);
$result[] = [
'org_id' => trim($dt[0]),
'site' => trim($dt[1])
];
}
} catch (\Exception $e) {
Printer::println($e->getMessage(), Color::RED);
} finally {
return $result;
}
}
}