Am i doing this right?

Published 6 months ago by Riotsmurf

I am creating business logic in my Laravel project. I am creating an order and adding samples to the order. Each sample( depending on its type ) has a set of tests that it requires by law so i made this.

So i use getTestCollection() to return tests a product type needs.

<?php

namespace App\TestLogic;

class Product
{

    private $type;
    private $typeFactory;

    public $tests = [
            "Foreign Matter" => 0,
            "Microbial" => 0,
            "Moisture" => 0,
            "Mycotoxin" => 0,
            "Pesticide Residue" => 0,
            "Potency" => 0,
            "Residual Solvent" => 0,
            "Terpenes" => 0,
            "Water Activity" => 0
    ];

    public function __construct(ProductTypeFactory $typeFactory)
    {
        $this->typeFactory = $typeFactory;
    }
    /**
     * Gets a collection of tests based on the type of this product
     * @return Array  collection of tests required by type.
     */
    public function getTestCollection()
    {
        $classType = str_replace(' ', '',$this->type);
        $productType = $this->typeFactory->getProductTestType($classType);

        return $productType->getTestCollection($this);
    }

    public function setType($type)
    {
        $this->type = $type;
    }

    public function getType()
    {
        return $this->type;
    }
}

There are some types that require the same set of tests like, Flower and Flower Mix. So i made this.

<?php

namespace App\TestLogic;

class VegetationType implements IproductTypeTest
{
    protected $requiredTests = [
            "Moisture" => 1,
            "Water Activity" => 1,
            "Potency" => 1,
            "Foreign Matter" => 1,
            "Microbial" => 1
            // "Mycotoxins"
    ];

    public function getTestCollection(Product $product)
    {
        foreach ($this->requiredTests as $key => $rTest) {
                $product->tests[$key]+=$rTest;
        }
        return $product->tests;
    }
}

And my Flower, Flower Mix classes extend this. So now they are empty like this.

<?php

namespace App\TestLogic;

class FlowerMixTypeTest extends VegetationType
{
}

<?php

namespace App\TestLogic;

class FlowerTypeTest extends VegetationType
{
}

Should VegitationType be a trait and not a class? This is my first go at this and i have a feeling its a little weird...

jlrdw
jlrdw
6 months ago (200,550 XP)

Two good ways to handle "same" fields applying is polymorphic relations https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations and the old school way which is still 100% fine is a checkbox or radio button.

This set of data applies to this or that, via a radio button. I just like the checkbox better.

Riotsmurf

jlrdw: This is a select, not a checkbox or radio button. there are 6 parent types. VegetationType, ExtractType, TopicalType etc. I am not going to force the users to select "Flower" or "Flower Mix" and then check a radio button to pick vegetation because it leaves too much room for user error. This does not answer my question.

jlrdw
jlrdw
6 months ago (200,550 XP)

This is a select, not a checkbox or radio button

Still same idea instead of a check(0 or 1) you still have a "certain word" describe what set of data it is. Also index that column, and later you can query where ("that select option").

Sounds as though you are pretty much setup with what's needed.

Snapey
Snapey
6 months ago (713,295 XP)

seems like a bad idea to store such behaviour in code

It would be better to have the tests maintained in a database model.

As it stands, if flower mix no longer required moisture test, you have to change your code and redeploy.

Riotsmurf

Snapey: Hmm i see what you mean.

We had a database idea at first but then felt like we didn't want to store this information because its just requirements that are set by the state. We felt like the law does not change enough, and if it did it would not be a big deal to go into the FlowerMixTypeTest class and set the $requiredTests variable to what it would require. Then commit, push, and merge. Would you consider this bad still?

I suppose it would be about which is faster/easier for the system. Whether it is easier to store and update this information in the database, or less time to change 1 class.

Please sign in or create an account to participate in this conversation.