# Implementation of Plan_Item_Interface

    <?php

    /**
     * Sample implementation of a Service Plan Item declaration.
     *
     */

    /**
     *
     * Plan_Item_Interface declares the following methods:
     *
     *   public function getName();
     *   public function getHint($locale);
     *   public function getDescription($locale);
     *   public function update(
     *       $subject,
     *       $change,
     *       $subscriptionUuid,
     *       $planItemUuid
     *   );
     *
     * Additional descriptions are available as annotations to method
     * implementations below.
     *
     */
    class servicedir_servicefile implements Plan_Item_Interface
    {
        private $_logFile = '/tmp/custom-item-connector.log';

        private $_locales = array(
            'en-US' => array(
                'description' => 'External Mail Filtering Service',
                'hint' => 'Filter all incoming mail throug cloud mail filter',
            ),
            'de-DE' => array(
                'description' => 'This is a description in German',
                'hint' => 'And hint is also in German',
            ),
        );

        /**
         * Returns a unique name of an additional service.
         * Plesk uses this name to distinguish services from each other.
         *
         *
         * @return string
         */
        public function getName()
        {
            return 'urn:isv:custom-item-connector:1';
        }

        /**
         * Returns a localized name of the service, that will be
         * displayed in Plesk (in Service Plans > Additional Services).
         *
         *
         * @param string $locale Currently used locale in format 'xx-XX' (RFC 1766).
         * @return string
         */
        public function getHint($locale)
        {
            return $this->_('hint', $locale);
        }

        /**
         * Returns a localized name of the service, that will be
         * displayed in Plesk (in Service Plans > Additional Services).
         *
         *
         * @param string $locale Currently used locale in format 'xx-XX'
         * @return string
         */
        public function getDescription($locale)
        {
            return $this->_('description', $locale);
        }

        /**
         * Receives update notifications about related Plesk objects. The objects are
         * specified with object implementing Plan_Item_Subject interface that
         * declares the following methods:
         *
         *   public function getType() - Returns type of a changed object
         *        as specified by Plan_Item_Subject constants
         *
         *          Available types: Plan_Item_Subject::EMAIL, Plan_Item_Subject::SITE,
         *          Plan_Item_Subject::SUBSCRIPTION
         *
         *
         *   public function getProperties() - Returns the current values of
         *       subject properties. The properties are returned as key => value pairs.
         *      For deleted objects, properties values remain as they were
         *      before deletion. For created objects, properties values are those
         *      that were assigned to these objects upon creation.
         *
         *
         *      Available properties.
         *
         *      For subscriptions:
         *        GUID: string
         *        status: boolean
         *
         *      For sites:
         *        status: boolean
         *        name: string
         *        GUID: string
         *
         *      For e-mail accounts:
         *        email: string (account name)

         *
         * Changes are specified with $change that is one of
         * Plan_Item_Notification::{CREATED,UPDATED,DELETED} constants.
         *
         * $subscriptionUuid is a GUID of subscription.
         *
         * $planItemUuid is a GUID of the additional service.
         *
         *
         * @param Plan_Item_Subject $subject
         * @param int $change
         * @param string $subscriptionUuid
         * @param string $planItemUuid
         * @return void
         */
        public function update(
            Plan_Item_Subject $subject,
            $change,
            $subscriptionUuid,
            $planItemUuid
        )
        {
            $subjectType = Plan_Item_Subject::EMAIL === $subject->getType()
                ? 'Email address'
                : 'unknown';

            switch ($change) {

                case Plan_Item_Notification::CREATED:
                    $this->_log("$subjectType created: " .
                        print_r($subject->getProperties(), true)
                    );
                    break;

                case Plan_Item_Notification::UPDATED:
                    $this->_log("$subjectType updated: " .
                        print_r($subject->getProperties(), true)
                    );
                    break;

                case Plan_Item_Notification::DELETED:
                    $this->_log("$subjectType deleted: " .
                        print_r($subject->getProperties(), true)
                    );
                    break;
            }
        }

        /**
         * Declares from what type of Plesk objects the service will receive notifications.
         * It returns either an array or a single type.
         *
         * Plan_Item_Subject::SUBSCRIPTION
         *      The service will recieve notifications about all changes made to
         *    subscription (including activation/suspension of subscriptions,
         *    assigning/de-assigning the service to a subscription)
         *
         * Plan_Item_Subject::EMAIL
         *      The service will recieve notifications about all changes made to
         *    e-mail accounts on affected subscriptions.
         *
         * Plan_Item_Subject::SITE
         *      The service will recieve notifications about all changes made to
         *    site names in the scope affected subscriptions.
         *
         */
        public static function getSubjectTypes()
        {
            return array(Plan_Item_Subject::EMAIL);
        }

        private function _($key, $locale)
        {
            if (!isset($this->_locales[$locale])) {
                return $this->_locales['en-US'][$key];
            }

            return $this->_locales[$locale][$key];
        }

        private function _log($message)
        {
            date_default_timezone_set('UTC');

            $logString = "[" . date("H:i:s") . "] " . $message;

            file_put_contents($this->_logFile, $message, FILE_APPEND);
        }
    }