-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseImporter.php
More file actions
100 lines (82 loc) · 2.66 KB
/
BaseImporter.php
File metadata and controls
100 lines (82 loc) · 2.66 KB
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
<?php
namespace App\Service;
use App\Repository\GroupRepository;
use App\Repository\ReportRepository;
use App\Repository\SystemRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
abstract class BaseImporter implements ImportInterface
{
protected string $url;
public function __construct(
protected ReportRepository $reportRepository,
protected SystemRepository $systemRepository,
protected GroupRepository $groupRepository,
protected EntityManagerInterface $entityManager,
protected ParameterBagInterface $params,
) {
$this->url = $this->params->get('system_url') ?? '';
}
public function import(string $src, ?ProgressBar $progressBar = null): void
{
// We need to be able to find all entities during import.
$this->entityManager->getFilters()->disable('entity_active');
$this->doImport($src, $progressBar);
}
abstract protected function doImport(string $src, ?ProgressBar $progressBar): void;
protected function sanitizeText(string $str): ?string
{
$str = strip_tags($str, '<p><div><strong><a><ul><li><span><br><br/>');
$str = preg_replace("/<([a-z][a-z0-9]*)(?:[^>]*(\shref=['\"][^'\"]*['\"]))?[^>]*?(\/?)>/i", '<$1$2$3>', $str);
$str = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http|mailto)([^\"'>]+)([\"'>]+)#", '$1'.$this->url.'$2$3', (string) $str);
return $str;
}
/**
* @param array<int,mixed>|null $list
*
* @return string|null
*/
protected function convertList(?array $list): ?string
{
if ($list) {
return implode(', ', $list);
}
return '';
}
/**
* @param $obj
*
* @return string|null
*/
protected function convertLink(?object $obj): ?string
{
if ($obj && $obj->Url && $obj->Description) {
return '<a href="'.htmlspecialchars($obj->Url).'">'.$obj->Description.'</a>';
}
return '';
}
/**
* @throws \Exception
*/
protected function convertDate(string $date): ?\DateTimeInterface
{
return empty($date) ? null : new \DateTimeImmutable($date);
}
/**
* @param array<int,object> $systemOwner
*
* @return string
*/
protected function convertSystemOwner(array $systemOwner): string
{
if (empty($systemOwner)) {
return '';
}
return $systemOwner[0]->LookupValue ?? '';
}
protected function convertBoolean(string $str): bool
{
return 'true' == $str;
}
}