-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust PSR-4 compliance to work with Composer 2 (#37)
* Correct PSR-4 compliance to work with Composer 2 * Unify HttpAsset and HttpAssetInterface
- Loading branch information
Showing
46 changed files
with
221 additions
and
195 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,181 +1,15 @@ | ||
<?php | ||
/** | ||
* @date 08/03/2019 | ||
* @author Wojciech Peisert <[email protected]> | ||
* @copyright Copyright (c) 2018 DIVANTE (http://divante.co) | ||
* @author Wojciech Peisert <[email protected]> | ||
*/ | ||
|
||
namespace PimcoreDevkitBundle\PimcoreDevkitBundle\Wrapper; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
use Pimcore\Model\Asset; | ||
use Pimcore\Model\Asset\Folder; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Symfony\Component\Filesystem\Exception\FileNotFoundException; | ||
|
||
/** | ||
* Class HttpAsset | ||
* @package PimcoreDevkitBundle\PimcoreDevkitBundle\Wrapper | ||
* @deprecated Use \PimcoreDevkitBundle\Wrapper\HttpAsset instead | ||
*/ | ||
class HttpAsset implements HttpAssetInterface | ||
class HttpAsset extends \PimcoreDevkitBundle\Wrapper\HttpAsset implements HttpAssetInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $url; | ||
|
||
/** | ||
* @var ResponseInterface | ||
*/ | ||
protected $response; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $filename; | ||
|
||
/** | ||
* @var Folder | ||
*/ | ||
protected $targetFolder; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $defaultFileType; | ||
|
||
/** | ||
* @var Asset | ||
*/ | ||
protected $asset; | ||
|
||
/** | ||
* HttpAsset constructor. | ||
* @param ClientInterface $httpClient | ||
* @param string $url | ||
* @param Folder $targetFolder | ||
* @param string|null $filename | ||
* @param string $defaultFileType | ||
* @throws \GuzzleHttp\Exception\GuzzleException | ||
*/ | ||
public function __construct( | ||
ClientInterface $httpClient, | ||
string $url, | ||
Folder $targetFolder, | ||
string $filename = null, | ||
string $defaultFileType = 'txt', | ||
array $headers = [] | ||
) { | ||
$this->response = $httpClient->request('GET', $url, ['headers' => $headers, 'connect_timeout' => 1]); | ||
$statusCode = $this->response->getStatusCode(); | ||
|
||
if ($statusCode != 200) { | ||
throw new FileNotFoundException("Error $statusCode while downloading url: $url"); | ||
} | ||
|
||
$this->setAssetFilename($filename); | ||
$this->targetFolder = $targetFolder; | ||
$this->defaultFileType = $defaultFileType; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFileType(): string | ||
{ | ||
return strval(array_search( | ||
$this->getContentType(), | ||
\Pimcore::getContainer()->getParameter('pimcore.mime.extensions') | ||
)); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getContentType(): string | ||
{ | ||
return strval($this->getHeader('content-type')[0]); | ||
} | ||
|
||
/** | ||
* @return Asset | ||
* @throws \Exception | ||
*/ | ||
public function getAsset(): Asset | ||
{ | ||
if (!$this->asset) { | ||
$this->saveAsset(); | ||
} | ||
|
||
return $this->asset; | ||
} | ||
|
||
/** | ||
* @return self | ||
* @throws \Exception | ||
*/ | ||
protected function saveAsset(): self | ||
{ | ||
$asset = Asset::getByPath($this->targetFolder->getFullPath() . "/" . $this->filename); | ||
|
||
if (!$asset) { | ||
$asset = new Asset(); | ||
$asset->setFilename($this->filename); | ||
$asset->setParent($this->targetFolder); | ||
$asset->addMetadata("origin", "text", "url"); | ||
$asset->addMetadata("origin_url", "text", $this->url); | ||
$asset->setData($this->getBody()); | ||
$asset->save(); | ||
} | ||
|
||
$this->asset = $asset; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $filename | ||
* @return void | ||
*/ | ||
protected function setAssetFilename(?string $filename) | ||
{ | ||
if ($filename) { | ||
$this->filename = $filename; | ||
return; | ||
} | ||
|
||
$this->generateAssetFilename(); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function generateAssetFilename(): void | ||
{ | ||
$fileType = $this->getFileType(); | ||
|
||
if (false === $fileType) { | ||
$fileType = $this->defaultFileType; | ||
} | ||
|
||
$this->filename = md5((string) $this->getBody()) . "." . $fileType; | ||
} | ||
|
||
/** | ||
* @return StreamInterface | ||
*/ | ||
protected function getBody(): StreamInterface | ||
{ | ||
return $this->response->getBody(); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return string[] | ||
*/ | ||
protected function getHeader(string $name): array | ||
{ | ||
return $this->response->getHeader($name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,15 @@ | ||
<?php | ||
/** | ||
* @date 08/03/2019 | ||
* @author Wojciech Peisert <[email protected]> | ||
* @copyright Copyright (c) 2018 DIVANTE (http://divante.co) | ||
* @author Wojciech Peisert <[email protected]> | ||
*/ | ||
|
||
namespace PimcoreDevkitBundle\PimcoreDevkitBundle\Wrapper; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
use Pimcore\Model\Asset; | ||
use Pimcore\Model\Asset\Folder; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Symfony\Component\Filesystem\Exception\FileNotFoundException; | ||
|
||
/** | ||
* Interface HttpAssetInterface | ||
* @package PimcoreDevkitBundle\PimcoreDevkitBundle\Wrapper | ||
* @deprecated Use \PimcoreDevkitBundle\Wrapper\HttpAssetInterface instead | ||
*/ | ||
interface HttpAssetInterface | ||
interface HttpAssetInterface extends \PimcoreDevkitBundle\Wrapper\HttpAssetInterface | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function getFileType(): string; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getContentType(): string; | ||
|
||
/** | ||
* @return Asset | ||
* @throws \Exception | ||
*/ | ||
public function getAsset(): Asset; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.