splObserver and Centos 5.11 / Centos 7

General support questions including new installations
Post Reply
rwap
Posts: 20
Joined: 2010/10/28 16:49:33
Location: Stone, UK
Contact:

splObserver and Centos 5.11 / Centos 7

Post by rwap » 2015/04/30 12:08:36

I have a site which uses the SplObserver class to add events and then notify various users by email about those events.

However, I am having a problem with the code:

Code: Select all

    /**
     * Attach a Observer to Offer
     * @param SplObserver $obs Observer instance
     *
     * return void
     */
    public function attach(SplObserver $obs)
    {
        $id = spl_object_hash($obs);
        $this->observers[$id] = $obs;
    }

    /**
     * Remove a Observer to Offer
     * @param SplObserver $obs Observer instance
     *
     * @return void
     */
    public function detach(SplObserver $obs)
    {
        $id = spl_object_hash($obs);
        unset($this->observers[$id]);
    }

    /**
     * Notify attached Observer
     */
    public function notify()
    {
        if ($this->observers) {
            foreach ($this->observers as $obs) {
                $this->detach($obs);
                $obs->update($this);
            }
        }
        $this->lastEvent = null;
    }

    public function prepareCommonObservers(MyPDO $DB, Core_Context $context, $extraInfo=null)
    {
        //Mails
        require_once ('class/contrib/phpmailer/class.phpmailer.php');
        $notificator = new MailNotificator(new PHPMailer(),new FileReader(),$context);
        $mailObs = OfferObserver_OfferMail::getInstance($this->type(), $context, $notificator, $DB);
        $mailObs->extraInfo=$extraInfo;
        $this->attach($mailObs);

        //Business Listing Limits
        $keyObs = new OfferObserver_BusinessLimitObserver($context, $notificator, $DB);
        $this->attach($keyObs);

        //Keywords
        $keyObs = new OfferObserver_KeywordObserver($context, $notificator, $DB);
        $this->attach($keyObs);

        //Banned Words
        $bannedWordObs = new OfferObserver_BannedWordsObserver($DB);
        $this->attach($bannedWordObs);

        //Final value fee
        if ($this->item->user->businessSeller) {
        	$finalFee = BusinessFee::getInstanceFromDB($DB, 'offer/finalValue', $this->originalSiteId, $context->siteOptions['currentChargeStructure']);
        } else {
	        $finalFee = Fee::getInstanceFromDB($DB, 'offer/finalValue', $this->originalSiteId, $context->siteOptions['currentChargeStructure']);
        }
        if ($finalFee->active) {
            $finalValueObs = new OfferObserver_FinalValueFeeObserver($context, $notificator, $DB);
            $this->attach($finalValueObs);
        }

        //Auction Item queue
        $itemQueueObs = new OfferObserver_AuctionQueue($DB);
        $this->attach($itemQueueObs);
    }
In particular, for two sites running the same code on Centos 5, I have had to alter the original:

Code: Select all

    public function notify()
    {
        if ($this->observers) {
            foreach ($this->observers as $obs) {
                $obs->update($this);
            }
        }
    }
to read:

Code: Select all

    public function notify()
    {
        if ($this->observers) {
            foreach ($this->observers as $obs) {
                $this->detach($obs);
                $obs->update($this);
            }
        }
        $this->lastEvent = null;
    }
Without this change, I was often getting duplicated lots of emails sent out. Both servers are running Centos 5.11, although different PHP versions (5.5 and 5.6).

However, on another site running on Centos 7 (PHP 5.6), I have to revert to the original function, otherwise emails are not sent out...

Any suggestions as to what may be happening?

Post Reply