In laravel 10 / PHP 8.2 app I made an interface class
namespace AppLibraryServicesInterfaces;
use AppModelsNews;
use CarbonCarbon;
/**
* Interface of Class to calculate rating of news
*/
interface NewsRatingInterface
{
/**
* calculate internal rating of a news
*
* @param int|News $news
*
* @return float - calculated news internal rating
*/
public function calc(int|News $news): float;
/**
* get membership mark bonus depending on creator/user membership_mark
*
* @return float - membership mark bonus
*/
function getMembershipMarkBonus(): float;
/**
* get total comments Weight(for all time period) for 1 comment depending on creator/user membership_mark
*
* @return float - total comments Weight
*/
function getTotalCommentsWeight(): float;
/**
* get today comments Weight(for today) for 1 comment depending on creator/user membership_mark
*
* @return float - today comments Weight
*/
function getTodayCommentsWeight(): float;
...
and its implementation in app/Library/Services/NewsRatingForMemberUser.php :
<?php
namespace AppLibraryServices;
use AppLibraryServicesInterfacesNewsRatingInterface;
use AppModelsNews;
class NewsRatingForMemberUser implements NewsRatingInterface
{
public $news;
public $reactionsWeight = 1;
public $commentsWeight = 1;
/**
* calculate internal rating of a news
*
* @param int|News $news
*
* @return float - calculated news internal rating
*/
public function calc(int|News $news): float
{
Log::info(varDump(is_int($news), ' -1 NewsRatingForMemberUser calc is_int($news)::'));
...
return self::calcReactions() + self::getMembershipMarkBonus() + self::calcComments() +
self::calcTodayComments($todayDate) + self::calcLastWeekComments($todayDate) /*$lastWeekCommentsSummary */;
}
/**
* get membership mark bonus depending on creator/user membership_mark
*
* @return float - membership mark bonus
*/
function getMembershipMarkBonus(): float {
Log::info(varDump(0, ' -1 NewsRatingForMemberUser User getMembershipMarkBonus 0::'));
return 0; // Simple member has no any points bonus
}
/**
* get total comments Weight(for all time period) for 1 comment depending on creator/user membership_mark
*
* @return float - total comments Weight
*/
function getTotalCommentsWeight(): float
{
return 0.2;
}
/**
* get today comments Weight(for today) for 1 comment depending on creator/user membership_mark
*
* @return float - today comments Weight
*/
function getTodayCommentsWeight(): float {
return 0.5;
}
I created a third class in app/Library/Services/NewsRatingForGoldUser.php which extends NewsRatingForMemberUser :
<?php
namespace AppLibraryServices;
use AppModelsNews;
use AppLibraryServicesNewsRatingForMemberUser;
class NewsRatingForGoldUser extends NewsRatingForMemberUser
{
/**
* get membership mark bonus depending on creator/user membership_mark
*
* @return float - membership mark bonus
*/
function getMembershipMarkBonus(): float {
Log::info(varDump(50, ' -1 NewsRatingForGold User getMembershipMarkBonus 50::'));
die("getMembershipMarkBonus - 50");
return 50;
}
/**
* get total comments Weight(for all time period) for 1 comment depending on creator/user membership_mark
*
* @return float - total comments Weight
*/
function getTotalCommentsWeight(): float
{
die("getTotalCommentsWeight - 50");
return parent::getTotalCommentsWeight() * 2;
}
/**
* get today comments Weight(for today) for 1 comment depending on creator/user membership_mark
*
* @return float - today comments Weight
*/
function getTodayCommentsWeight(): float {
die("getLastWeekCommentsWeight - 50");
return parent::getTodayCommentsWeight() * 2;
}
/**
* get last week comments Weight(for last week) for 1 comment depending on creator/user membership_mark
*
* @return float - last week comments Weight
*/
function getLastWeekCommentsWeight(): float {
die("getLastWeekCommentsWeight - 50");
return parent::getLastWeekCommentsWeight() * 2;
}
}
I use model attribute in model app/Models/News.php like :
public function getRatingAttribute()
{
$newsRatingMembershipMarkClasses = 'news_rating_membership_mark_classes' => [
'M' => 'AppLibraryServicesNewsRatingForMemberUser',
'G' => 'AppLibraryServicesNewsRatingForGoldUser',
];
return (new $newsRatingMembershipMarkClasses[$this->creator->membership_mark])-> calc(news: $this->id);
}
Problem is when NewsRatingForGoldUser class is binded (field membership_mark has ‘G’ value) method getMembershipMarkBonus is called from NewsRatingForMemberUser,
but NOT from NewsRatingForGoldUser class (I my example above it calls die method – I do not see this message.
What is wrong with getMembershipMarkBonus of NewsRatingForGoldUser method and why it is not called?