Strange circular dependency in Symfony 5

I am having a problem with Symfony 5 and its Circular Dependency detection mechanism.

I have defined the following class

namespace AppHTSCEntityFinanceSubscription;

/**
 * Category
 */
class Category {
    /**
     * @var string
     */
    private $name;

    /**
     * @var integer
     */
    private $version = 1;

    /**
     * @var integer
     */
    private $id;

    /**
     * @var array
     */
    private $fees;

    /**
     * Map of feeType => fee
     * @var array
     */
    private $feeMap = null;
    
    /**
     * Constructor
     */
    public function __construct(Category $category = null)
    {
        if (!is_null($category)) {
            $this->name = $category->name;
            $this->setFees($category->fees);
        } else {
            $this->fees = array();
        }
    }
    
    /**
     * Set name
     *
     * @param string $name
     * @return Category
     * @throws InvalidArgumentException
     */
    public function setName($name)
    {
        if (is_null($name) || empty($name = trim($name))) {
            return InvalidArgumentException('Empty category name');
        }
        $this->name = $name;
    
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set version
     *
     * @param integer $version
     * @return Category
     */
    public function setVersion($version)
    {
        $this->version = $version;
    
        return $this;
    }

    /**
     * Get version
     *
     * @return integer 
     */
    public function getVersion()
    {
        return $this->version;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
   
    /**
     * Sets an array of fees to the category.
     * 
     * @param array $fees
     * @return Category
     */
    public function setFees(array $fees) {
        $this->fees = array();
        $this->feeMap = array();
        
        foreach ($fees as $fee) {
            $this->addFee($fee);
        }   
        
        return $this;
    }
    
    /**
     * Add fee to category. Only one fee per fee type allowed. 
     *
     * @param Fee $fee
     * @return Category
     */
    public function addFee(Fee $fee)
    {
        if (!$fee->isValid()) throw new InvalidArgumentException('Invalid fee added to category');
        
        $feeMap = $this->getFeeMap();
        if (key_exists($fee->getType()->getName(), $feeMap)) {
            throw new InvalidArgumentException('Multiple fee type [' . $fee->getType()->getName() . '] specified for category [' . $this->name . ']');
        }
        $this->fees[] = $fee;
        $this->feeMap[$fee->getType()->getName()] = $fee;
    
        return $this;
    }

    /**
     * Remove fee
     *
     * @param Fee $fee
     * @return Category
     */
    public function removeFee(Fee $fee)
    {
        $this->fees->removeElement($fee);
        
        return $this;
    }
    
    /**
     * Get fees
     *
     * @return Fee[]
     */
    public function getFees()
    {
        return $this->fees;
    }

    /**
     * Get fee of given type. Returns null if type is not present.
     *
     * @return Fee 
     */
    public function getFee(FeeType $type) 
    {
        $feeMap = $this->getFeeMap();
        if (key_exists($type->getName(), $feeMap)) return $feeMap[$type->getName()];
        return null;
    }
    
    private function getFeeMap() {
        if (is_null($this->feeMap)) {
            $this->feeMap = array();
            foreach ($this->fees as $fee) $this->feeMap[$fee->getType()->getName()] = $fee;
        }
        return $this->feeMap;
    }
    /**
     * Returns whether a category is considered valid.
     * @return boolean
     */
    public function isValid() {
        if (empty($this->name)) return false;
        if (empty($this->fees)) return false;
        return true;
    }
}

When I access the web site and hence my code above, Symfony responds with the following message:

Circular reference detected for service "AppHTSCEntityFinanceSubscriptionCategory", path: "AppHTSCEntityFinanceSubscriptionCategory -> AppHTSCEntityFinanceSubscriptionCategory".

Normally when a circular dependency is detected, a second class is involved; in my case it is a circular dependency between the class itself?

I also inspected other classes using this class, but cannot determine a circular dependency with the this class anywhere.

Anybody experienced the same problem and knows a solution for this?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *