Courses

How to Structure Laravel 9 Projects

O: Open-Closed Principle - Eloquent Attribute with Salary Calculator

Previous: O: Open-Closed Principle - Vendor in Laravel
avatar

It's getting a bit tricky. You have created App/Calculators folder with calculator files/classes in it. How you would call them - is it Actions, is it Helpers? What are those classes?

👍 1
avatar
You can use Markdown
avatar

Instead of having an associative array matching the position and the corresponding class, can we not use the interface? I think we'll have somthing like this

public function getSalaryAttribute(SalaryCalculatorInterface $salarayCalculator) {

	 //And then for any type of position, we'll juste have to do 
	 $salarayCalculator->calculate($this->start_date)
	 
}

What do you think ?

avatar

Hmm, I think it's quite a good idea, can't answer 100% without trying it out but I think that would work.

avatar

Alright Povilas Korop.Thanks

avatar
You can use Markdown
avatar

I really don't like this example. If you want to check the consistency of your salary policy, you have to open a file for each employee type and layout them in such a way that you have an overview. Having all in one file violates the open-closed principle but is much faster to read, check, compare, ....

avatar
You can use Markdown
avatar
You can use Markdown