/
UrlStorage.php
143 lines (122 loc) · 4.14 KB
/
UrlStorage.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\FieldType\Url;
use eZ\Publish\SPI\FieldType\GatewayBasedStorage;
use eZ\Publish\SPI\FieldType\StorageGateway;
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
use eZ\Publish\SPI\Persistence\Content\Field;
use Psr\Log\LoggerInterface;
/**
* Converter for Url field type external storage.
*/
class UrlStorage extends GatewayBasedStorage
{
/** @var \Psr\Log\LoggerInterface */
protected $logger;
/** @var \eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway */
protected $gateway;
/**
* Construct from gateways.
*
* @param \eZ\Publish\SPI\FieldType\StorageGateway $gateway
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(StorageGateway $gateway, LoggerInterface $logger = null)
{
parent::__construct($gateway);
$this->logger = $logger;
}
/**
* @see \eZ\Publish\SPI\FieldType\FieldStorage
*
* @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
* @param \eZ\Publish\SPI\Persistence\Content\Field $field
* @param array $context
*
* @return bool|mixed
*/
public function storeFieldData(VersionInfo $versionInfo, Field $field, array $context)
{
$url = $field->value->externalData;
if (empty($url)) {
return false;
}
$map = $this->gateway->getUrlIdMap([$url]);
if (isset($map[$url])) {
$urlId = $map[$url];
} else {
$urlId = $this->gateway->insertUrl($url);
}
$this->gateway->linkUrl($urlId, $field->id, $versionInfo->versionNo);
$this->gateway->unlinkUrl(
$field->id,
$versionInfo->versionNo,
[$urlId]
);
$field->value->data['urlId'] = $urlId;
// Signals that the Value has been modified and that an update is to be performed
return true;
}
/**
* Populates $field value property based on the external data.
* $field->value is a {@link eZ\Publish\SPI\Persistence\Content\FieldValue} object.
* This value holds the data as a {@link eZ\Publish\Core\FieldType\Value} based object,
* according to the field type (e.g. for TextLine, it will be a {@link eZ\Publish\Core\FieldType\TextLine\Value} object).
*
* @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
* @param \eZ\Publish\SPI\Persistence\Content\Field $field
* @param array $context
*/
public function getFieldData(VersionInfo $versionInfo, Field $field, array $context)
{
$id = $field->value->data['urlId'];
if (empty($id)) {
$field->value->externalData = null;
return;
}
$map = $this->gateway->getIdUrlMap([$id]);
// URL id is not in the DB
if (!isset($map[$id]) && isset($this->logger)) {
$this->logger->error("URL with ID '{$id}' not found");
}
$field->value->externalData = isset($map[$id]) ? $map[$id] : '';
}
/**
* Deletes field data for all $fieldIds in the version identified by
* $versionInfo.
*
* @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
* @param array $fieldIds
* @param array $context
*
* @return bool
*/
public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds, array $context)
{
foreach ($fieldIds as $fieldId) {
$this->gateway->unlinkUrl($fieldId, $versionInfo->versionNo);
}
}
/**
* Checks if field type has external data to deal with.
*
* @return bool
*/
public function hasFieldData()
{
return true;
}
/**
* @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
* @param \eZ\Publish\SPI\Persistence\Content\Field $field
* @param array $context
*
* @return \eZ\Publish\SPI\Search\Field[]
*/
public function getIndexData(VersionInfo $versionInfo, Field $field, array $context)
{
}
}