Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Injecting a class constant.
  • Loading branch information
Grigori Kochanov committed Dec 20, 2020
1 parent aaccf2f commit b70a12b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 38 deletions.
2 changes: 2 additions & 0 deletions packages/acme-contracts/src/ImageInterface.php
Expand Up @@ -8,4 +8,6 @@
*/
interface ImageInterface
{
public function __construct(array $AVATAR_SIZE);
public function getUserAvatar(int $userId): string;
}
11 changes: 9 additions & 2 deletions packages/acme-image/src/ImageService.php
Expand Up @@ -21,10 +21,17 @@ final class ImageService implements ImageInterface
*/
private Image $imageLib;

public function __construct()
private int $avatarWidth, $avatarHeight;

public function __construct(array $AVATAR_SIZE)
{
$this->imageLib = new Image();

[$this->avatarWidth, $this->avatarHeight] = $AVATAR_SIZE;
}

public function getUserAvatar(int $userId): string
{
$file = '../storage/'.$userId.'jpg';
return $this->imageLib->generateThumbnail($file, $this->avatarWidth, $this->avatarHeight);
}
}
10 changes: 6 additions & 4 deletions packages/acme-image/src/lib/Image.php
Expand Up @@ -2,10 +2,6 @@

namespace Acme\Image\lib;

use Acme\Contracts\BaseModelInterface;
use Acme\Contracts\HelperBridgeInterface;
use Acme\Helper;

/**
* This class is the code is not coupled with a \BaseModel class anymore
*
Expand All @@ -17,5 +13,11 @@ class Image
public function __construct(){
}

public function generateThumbnail(string $file, int $width, int $height): string
{
//imagine an Imagick class here
$image = new ImagickFake($file);
return $image->thumbnailImage($width, $height);
}

}
@@ -1,11 +1,11 @@
<?php
namespace Acme;
namespace Acme\Image\lib;

/**
* A stub to make the demo work without image magic extension
* @package Acme
*/
class ImagicFake
class ImagickFake
{
public function __construct(private $file)
{
Expand Down
5 changes: 4 additions & 1 deletion services.php
Expand Up @@ -16,7 +16,10 @@

$services->set(ImageResizeController::class);

$services->set(ImageInterface::class, ImageService::class);
$services->set(ImageInterface::class, ImageService::class)
->bind('$AVATAR_SIZE',User::AVATAR_SIZE);
// in YAML it would be
// array $AVATAR_SIZE: !php/const User::AVATAR_SIZE

$services->set(User::class);
};
5 changes: 2 additions & 3 deletions src/ImageResizeController.php
Expand Up @@ -11,9 +11,8 @@ public function __construct(public ImageInterface $image, private User $user)
$this->user->setId((int)$_GET['id']??42);
}

public function getAvatar()
public function getAvatar(): string
{
$imageResizer = new ImageResizer();
return $imageResizer->getUserAvatar($this->user);
return $this->image->getUserAvatar($this->user->getId());
}
}
26 changes: 0 additions & 26 deletions src/ImageResizer.php

This file was deleted.

0 comments on commit b70a12b

Please sign in to comment.