Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Fixes for shells. PHPStan 7
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 4, 2020
1 parent 5e39467 commit 9dcf806
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 107 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ Optional but required if you want image processing out of the box:

* The [Imagine Image processing plugin](https://github.com/burzum/cakephp-imagine-plugin) if you want to process and store images.
* [FlySystem](https://github.com/thephpleague/flysystem) as alternative library over Gaufrette
You can still implement whatever file processing you want very easy. It's not tied to Imagine.

You can still implement whatever file processing you want very easy. It's not tied to Imagine.

Documentation
-------------

For documentation, as well as tutorials, see the [docs](docs/Home.md) directory of this repository.
For documentation, as well as tutorials, see the [docs](docs/README.md) directory of this repository.

Support
-------
Expand Down
12 changes: 9 additions & 3 deletions docs/Documentation/How-To-Use.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ This class will listen to all the ImageStorage model events and save the uploade
It is important to understand that nearly each storage adapter requires a little different handling: Most of the time you can't treat a local file the same as a file you store in a cloud service.
The interface that this plugin and Gaufrette provide is the same but not the internals. So a path that works for your local file system might not work for your remote storage system because it has other requirements or limitations.

So if you want to store a file using Amazon S3 you would have to store it, create all the versions of that image locally and then upload each of them and then delete the local temp files. The good news is the plugin can already take care of that.

When you create a new listener it is important that you check the `model` field and the event subject object (usually a table object inheriting `\Cake\ORM\Table`) if it matches what you expect. Using the event system you could create any kind of storage and upload behavior without inheriting or touching the model code. Just write a listener class and attach it to the global EventManager.
So if you want to store a file using Amazon S3 you would have to store it,
create all the versions of that image locally and then upload each of them
and then delete the local temp files. The good news is the plugin can already take care of that.

When you create a new listener it is important that you check the `model` field and
the event subject object (usually a table object inheriting `\Cake\ORM\Table`) if it
matches what you expect.
Using the event system you could create any kind of storage and upload behavior without
inheriting or touching the model code. Just write a listener class and attach it to the global EventManager.

List of events
--------------
Expand Down
5 changes: 3 additions & 2 deletions docs/Home.md → docs/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Home


The **File Storage** plugin is giving you the possibility to store files in virtually any kind of storage backend. This plugin is wrapping the [Gaufrette](https://github.com/KnpLabs/Gaufrette) library in a CakePHP fashion and provides a simple way to use the storage adapters through the [StorageManager](../Lib/StorageManager.php) class.
The **File Storage** plugin is giving you the possibility to store files in virtually any kind of storage backend.
This plugin is wrapping the [Gaufrette](https://github.com/KnpLabs/Gaufrette) library in a CakePHP fashion
and provides a simple way to use the storage adapters through the [StorageManager](../Lib/StorageManager.php) class.

[See this list of included storage adapters.](Docs/Documentation/List-of-included-Adapters.md)

Expand Down
8 changes: 4 additions & 4 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0"?>
<ruleset name="PSR2-Fuck-Spaces">
<rule ref=".\vendor\cakephp\cakephp-codesniffer\CakePHP\ruleset.xml"/>
<rule ref="vendor/cakephp/cakephp-codesniffer/CakePHP/ruleset.xml"/>
<file>./src</file>
<file>./tests</file>
<rule ref="PSR2">
<exclude name="PSR2.Classes.ClassDeclaration"></exclude>
<exclude name="PSR2.Classes.ClassDeclaration"/>
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine" />
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine"/>
</rule>
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
<rule ref="Generic.WhiteSpace.ScopeIndent">
Expand All @@ -15,5 +15,5 @@
<property name="tabIndent" value="true"/>
</properties>
</rule>
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie" />
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie"/>
</ruleset>
9 changes: 7 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
parameters:
level: 4
autoload_files:
level: 7
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
bootstrapFiles:
- tests/bootstrap.php
earlyTerminatingMethodCalls:
Cake\Console\Shell:
- abort
10 changes: 5 additions & 5 deletions src/FileStorage/DataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function __construct(
}

/**
* @param \Cake\Datasource\EntityInterface $entity Entity
* @return \Phauthentic\Infrastructure\Storage\FileInterface File
* @param \Cake\Datasource\EntityInterface $entity
* @return \Phauthentic\Infrastructure\Storage\FileInterface
*/
public function entityToFileObject(EntityInterface $entity): FileInterface
{
Expand All @@ -49,7 +49,7 @@ public function entityToFileObject(EntityInterface $entity): FileInterface
}

if ($entity->has('file')) {
/** @var $uploadedFile \Psr\Http\Message\UploadedFileInterface|array */
/** @var \Psr\Http\Message\UploadedFileInterface|array $uploadedFile */
$uploadedFile = $entity->get('file');
if (!is_array($uploadedFile)) {
$filename = $uploadedFile->getStream()->getMetadata('uri');
Expand All @@ -64,8 +64,8 @@ public function entityToFileObject(EntityInterface $entity): FileInterface
}

/**
* @param \Phauthentic\Infrastructure\Storage\FileInterface $file File
* @param \Cake\Datasource\EntityInterface|null
* @param \Phauthentic\Infrastructure\Storage\FileInterface $file
* @param \Cake\Datasource\EntityInterface|null $entity
* @return \Cake\Datasource\EntityInterface
*/
public function fileObjectToEntity(FileInterface $file, ?EntityInterface $entity): EntityInterface
Expand Down
8 changes: 4 additions & 4 deletions src/FileStorage/DataTransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
interface DataTransformerInterface
{
/**
* @param \Cake\Datasource\EntityInterface $entity Entity
* @return \Phauthentic\Infrastructure\Storage\FileInterface File
* @param \Cake\Datasource\EntityInterface $entity
* @return \Phauthentic\Infrastructure\Storage\FileInterface
*/
public function entityToFileObject(EntityInterface $entity): FileInterface;

/**
* @param \Phauthentic\Infrastructure\Storage\FileInterface $file File
* @param \Cake\Datasource\EntityInterface|null
* @param \Phauthentic\Infrastructure\Storage\FileInterface $file
* @param \Cake\Datasource\EntityInterface|null $entity
* @return \Cake\Datasource\EntityInterface
*/
public function fileObjectToEntity(FileInterface $file, ?EntityInterface $entity): EntityInterface;
Expand Down
40 changes: 28 additions & 12 deletions src/Model/Behavior/FileStorageBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use League\Flysystem\AdapterInterface;
use Phauthentic\Infrastructure\Storage\FileInterface;
use Phauthentic\Infrastructure\Storage\FileStorage;
use Phauthentic\Infrastructure\Storage\Processor\ProcessorInterface;
use Phauthentic\Infrastructure\Storage\StorageServiceInterface;
use RuntimeException;
use Throwable;

Expand All @@ -35,9 +35,9 @@ class FileStorageBehavior extends Behavior
protected FileStorage $fileStorage;

/**
* @var \Phauthentic\Infrastructure\Storage\Processor\Image\ImageProcessor
* @var \Phauthentic\Infrastructure\Storage\Processor\ProcessorInterface
*/
protected ?ProcessorInterface $imageProcessor = null;
protected ?ProcessorInterface $imageProcessor;

/**
* @var \Burzum\FileStorage\FileStorage\DataTransformerInterface
Expand Down Expand Up @@ -77,10 +77,6 @@ public function initialize(array $config): void
);
}

if ($this->getConfig('imageProcessor') instanceOf ProcessorInterface) {
$this->imageProcessor = $this->getConfig('imageProcessor');
}

if (!$this->getConfig('dataTransformer') instanceof DataTransformerInterface) {
$this->transformer = new DataTransformer(
$this->getTable()
Expand All @@ -91,11 +87,11 @@ public function initialize(array $config): void
/**
* @throws \InvalidArgumentException
* @param string $configName
* @return array
* @return \League\Flysystem\AdapterInterface
*/
public function getStorageAdapter($configName)
public function getStorageAdapter(string $configName): AdapterInterface
{
$this->fileStorage->getStorage($configName);
return $this->fileStorage->getStorage($configName);
}

/**
Expand Down Expand Up @@ -303,7 +299,7 @@ public function entityToFileObject(EntityInterface $entity): FileInterface

/**
* @param \Phauthentic\Infrastructure\Storage\FileInterface $file File
* @param \Cake\Datasource\EntityInterface|null
* @param \Cake\Datasource\EntityInterface|null $entity
* @return \Cake\Datasource\EntityInterface
*/
public function fileObjectToEntity(FileInterface $file, ?EntityInterface $entity)
Expand All @@ -315,7 +311,7 @@ public function fileObjectToEntity(FileInterface $file, ?EntityInterface $entity
* Processes images
*
* @param \Phauthentic\Infrastructure\Storage\FileInterface $file File
* @param \Cake\Datasource\EntityInterface
* @param \Cake\Datasource\EntityInterface $entity
* @return \Phauthentic\Infrastructure\Storage\FileInterface
*/
public function processImages(FileInterface $file, EntityInterface $entity): FileInterface
Expand All @@ -333,4 +329,24 @@ public function processImages(FileInterface $file, EntityInterface $entity): Fil

return $file;
}

/**
* @return \Phauthentic\Infrastructure\Storage\Processor\ProcessorInterface
*/
protected function getImageProcessor(): ProcessorInterface
{
if ($this->imageProcessor !== null) {
return $this->imageProcessor;
}

if ($this->getConfig('imageProcessor') instanceOf ProcessorInterface) {
$this->imageProcessor = $this->getConfig('imageProcessor');
}

if ($this->imageProcessor === null) {
throw new RuntimeException('No image processor found');
}

return $this->imageProcessor;
}
}
Loading

0 comments on commit 9dcf806

Please sign in to comment.