Jump to content

Recommended Posts

The intent of the following script is to receive an ID in the endpoint, determine what type of record it represents, and create the appropriate object to deal with it.

There are two related aspects which I think should be done different, but I don't know how best to do them. Both require to be injected with some other data, and I've taken two approaches.

MyValidator is wrapped in a second closure and then invoked with the applicable data.

SpecificFactory is not wrapped in a second closure, but then needs an initializing init() method to make it usable.

Neither seem like the right approach.  Any recommendations?

require '../vendor/autoload.php';

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App(/* as applicable */);

$container = $app->getContainer();
$container['pdo'] = function () {/* as applicable */};

$container['myValidator'] = $container->factory(function ($container) {
    return function ($userData, $validationRules) use ($container) {
        return new MyValidator($userData, $validationRules, $container['pdo']);
    };
});

$container['specificFactory'] = function ($c) {
    return new SpecificFactory($c);
};

$app->put('/some/end/point{id:[0-9]+}', function (Request $request, Response $response, $args) {
    $factory=$this->specificFactory->init($args['id']);
    $rs=$factory->getService()->create($request->getParsedBody());
    return $factory->getResponder()->create($response, $rs);
});

$app->put('/some/other/end/point{id:[0-9]+}', function (Request $request, Response $response, $args) {
    $factory=$this->otherFactory->init($args['id']);
    $rs=$factory->getService()->create($request->getParsedBody());
    return $factory->getResponder()->create($response, $rs);
});

$app->run();
abstract class Factory
{
    protected $container, $properties;

    public function __construct(\Slim\Container $container) {
        $this->container=$container;
    }

    public function init($identifier) {
        if(ctype_digit($identifier)) {
            if(!$this->properties=$this->getProperties($identifier)) {
                throw new FactoryException("Invalid identifier ID '$id'.");
            }
        }
        elseif(is_string($identifier)) $this->properties=['type'=>$identifier];
        else throw new FactoryException('Invalid identifier');
        return $this;
    }

    public function getService() {
        if(!$this->properties) throw new FactoryException('Factory must be initialized first');
        if(!$service = $this->getServiceObject()) throw new FactoryException('Invalid entity type');
        return $service;
    }

    public function getResponder() {
        if(!$this->properties) throw new FactoryException('Factory must be initialized first');
        if(!$responder = $this->getResponderObject()) throw new FactoryException('Invalid entity type');
        return $responder;
    }

    abstract protected function getProperties($identifier);
    abstract protected function getServiceObject();
    abstract protected function getResponderObject();
}
class SpecificFactory extends Factory
{

    protected function getProperties($id) {
        $stmt = $this->container['pdo']->prepare(/* ... */);
        $stmt->execute([$id]);
        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }

    protected function getServiceObject() {
        switch($this->properties['type']) {
            case 'type1':
                return new Type1\ServiceType1(
                    new Type1\MapperType1(
                        $this->container['pdo'],
                        $this->properties
                    ),
                    ($this->container['validationService'])('rules1', $this->properties),
                    $this->properties
                );
                break;
            case 'type2':
                return new Type2\ServiceType2(
                    new Type2\MapperType2(
                        $this->container['pdo'],
                        $this->properties
                    ),
                    ($this->container['validationService'])('rules2', $this->properties),
                    $this->properties
                );
                break;
            case 'type3':
                return new Type3\ServiceType3(
                    new Type3\MapperType3(
                        $this->container['pdo'],
                        $this->properties
                    ),
                    ($this->container['validationService'])('rules3', $this->properties),
                    $this->properties
                );
                break;
            default: return false;
        }
    }

    protected function getResponderObject() {
        switch($this->properties['type']) {
            case 'type1':
                return new Type1\ResponderType1();
                break;
            case 'type2':
                return new Type2\ResponderType2();
                break;
            case 'type3':
                return new Type3\ResponderType3();
                break;
            default: return false;
        }
    }
}
class ServiceType1
{
    protected $mapper, $validator, $properties;
    public function __construct($mapper, $validator, $properties) {
        $this->mapper=$mapper;
        $this->validator=$validator;
        $this->properties=$properties;
    }

    public function update($userData) {
        $myInjectedValidator=($this->container['myValidator'])($userData, ['validationRules']);
        if($myInjectedValidator->isValid()){
            $this->mapper->update($userData);
        }
        else throw new Exception('bad data');
    }
}
class MyValidator
{
    protected $userData, $validationRules, $pdo;
    public function __construct($userData, $validationRules, $pdo) {
        $this->userData=$userData;
    }

    public function isValid() {
        return rand(1);
    }
}

 

Link to comment
https://forums.phpfreaks.com/topic/307413-using-slim-with-a-factory/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.