Skip to content

Instantly share code, notes, and snippets.

@bobbybouwmann
Last active March 7, 2016 11:39
Show Gist options
  • Select an option

  • Save bobbybouwmann/a8b33ebbeec33ccd64f5 to your computer and use it in GitHub Desktop.

Select an option

Save bobbybouwmann/a8b33ebbeec33ccd64f5 to your computer and use it in GitHub Desktop.

Revisions

  1. bobbybouwmann revised this gist Mar 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Assignment.md
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ says it has a documents function.

    So to fix that we can use multiple interfaces, like shown in the code examples. However this means that
    there are possibly around 20 interfaces and for each functionalty there needs to be a check on which
    provider should be used.
    provider should be used.

    So what we really want is a flexible way to call a method on one of the providers if that provider is
    supporting the functionality. So my question is how can we make a good architecture that can manage
  2. bobbybouwmann revised this gist Mar 7, 2016. 2 changed files with 22 additions and 1 deletion.
    20 changes: 20 additions & 0 deletions Assignment.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    So I’m building a system that connects to multiple HR-applications. So I was thinking about using the
    Builder (Manager) pattern, like socialite is doing. So you can say this user is using HR-application
    AFAS so I new up a AFASProvider and do all my calls in there. And then I simply need to create a
    provider for each HR-application we support and I give them the same interface and make sure they all
    return the same information structure so I can work with that.

    However the hard part here is that some HR-applications don’t support some functionalities that others
    do. So we were thinking about creating our own provider which can be used as well. So for example
    retrieve documents for a user is supported by AFAS but not by VISMA. So if the user is using the VISMA
    provider we need to make sure that for documents it’s using the local provider. Now that might not be
    that hard, but that means that the VISMA provider cannot use the normal interface, since the interface
    says it has a documents function.

    So to fix that we can use multiple interfaces, like shown in the code examples. However this means that
    there are possibly around 20 interfaces and for each functionalty there needs to be a check on which
    provider should be used.

    So what we really want is a flexible way to call a method on one of the providers if that provider is
    supporting the functionality. So my question is how can we make a good architecture that can manage
    this kind of behavior?
    3 changes: 2 additions & 1 deletion HRManager.php
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,6 @@ public function __construct(Application $app, $driver) {
    $this->driver = $driver;

    $this->driver($driver); // Create the import driver
    $this->driver('local'); // Create the backup driver
    }

    public function getProvider($interface)
    @@ -20,6 +19,8 @@ public function getProvider($interface)
    if ($provider instanceof $interface) {
    return $provider;
    }

    $this->driver('local'); // Create the backup driver if it doesn't exist

    return $this->getDrivers()[$this->getDefaultDriver()];
    }
  3. bobbybouwmann renamed this gist Mar 7, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. bobbybouwmann renamed this gist Mar 7, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. bobbybouwmann revised this gist Mar 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    $manager->documents(); // Get the documents from the provider
    $manager->user(); // Get the user from the provider

    // Expecte outcome
    // Expected outcome
    CreateVismaProvider
    CreateLocalProvider
    Local documents
  6. bobbybouwmann created this gist Mar 7, 2016.
    59 changes: 59 additions & 0 deletions HRManager.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    <?php

    class HRManager extends Manager
    {
    protected $driver;

    public function __construct(Application $app, $driver) {
    parent::__construct($app);

    $this->driver = $driver;

    $this->driver($driver); // Create the import driver
    $this->driver('local'); // Create the backup driver
    }

    public function getProvider($interface)
    {
    $provider = $this->getDrivers()[$this->driver];

    if ($provider instanceof $interface) {
    return $provider;
    }

    return $this->getDrivers()[$this->getDefaultDriver()];
    }

    public function documents()
    {
    $provider = $this->getProvider(DocumentProvider::class);

    $provider->documents();
    }

    public function user()
    {
    $provider = $this->getProvider(UserProvider::class);

    $provider->user();
    }

    public function createVismaDriver()
    {
    echo 'CreateVismaProvider<br>';

    return new VismaProvider();
    }

    public function createLocalDriver()
    {
    echo 'CreateLocalDriver<br>';

    return new LocalProvider();
    }

    public function getDefaultDriver()
    {
    return 'local';
    }
    }
    14 changes: 14 additions & 0 deletions LocalProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?php

    class LocalProvider implements DocumentProvider, UserProvider
    {
    public function documents()
    {
    echo 'Local documents<br>';
    }

    public function user()
    {
    echo 'Local user<br>';
    }
    }
    9 changes: 9 additions & 0 deletions VismaProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <?php

    class VismaProvider implements UserProvider
    {
    public function user()
    {
    echo 'Visma user<br>';
    }
    }
    13 changes: 13 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <?php

    // Create the manager that will generate the providers
    $manager = new \App\HR\HRManager(app(), 'visma');

    $manager->documents(); // Get the documents from the provider
    $manager->user(); // Get the user from the provider

    // Expecte outcome
    CreateVismaProvider
    CreateLocalProvider
    Local documents
    Visma user