class_WorksContract.php

Go to the documentation of this file.
00001 <?php
00024 class WorksContract extends BaseFrameworkSystem implements SignableContract {
00025         // Zukuenftiger Schiffsname
00026         private $shipName         = "";
00027 
00028         // Instanz zum Schiffstypen
00029         private $shipInstance     = null;
00030 
00031         // Contract partner
00032         private $contractPartner  = null;
00033 
00034         // Other contract partner
00035         private $contractParty    = null;
00036 
00037         // Is the contract signed?
00038         private $signed           = false;
00039 
00040         // Merchant instance
00041         private $merchantInstance = null;
00042 
00043         // Konstruktor
00044         protected function __construct () {
00045                 // Call parent constructor
00046                 parent::__construct(__CLASS__);
00047 
00048                 // Clean up a little
00049                 $this->removeSystemArray();
00050                 $this->removeNumberFormaters();
00051         }
00052 
00053         // Neuen Bauvertrag generieren
00054         public final static function createWorksContract ($shipType, $shipName, ContractPartner $partnerInstance) {
00055                 // Strings absichern
00056                 $shipType = (string) $shipType;
00057                 $shipName = (string) $shipName;
00058 
00059                 // Get new instance
00060                 $contractInstance = new WorksContract();
00061 
00062                 // Schiffsnamen setzen
00063                 $contractInstance->setShipName($shipName);
00064 
00065                 // Existiert die Klasse ueberhaupt?
00066                 if (!class_exists($shipType)) {
00067                         // Klasse nicht gefunden
00068                         throw new ClassNotFoundException ($shipType, self::EXCEPTION_CLASS_NOT_FOUND);
00069                 }
00070 
00071                 // Schiff-Instanz temporaer erzeugen und in den Bauvertrag einfuegen
00072                 $shipInstance = ObjectFactory::createObjectByName($shipType, array($shipName));
00073                 $contractInstance->setShipInstance($shipInstance);
00074 
00075                 // Remove the ship instance
00076                 unset($shipInstance);
00077 
00078                 // Set itself as contract partner
00079                 $contractInstance->setContractPartner($partnerInstance);
00080 
00081                 // Instanz zurueckgeben
00082                 return $contractInstance;
00083         }
00084 
00085         // Setter for ship instance
00086         private final function setShipInstance (ConstructableShip $shipInstance) {
00087                 $this->shipInstance = $shipInstance;
00088         }
00089 
00090         // Setter for ship name
00091         private final function setShipName ($shipName) {
00092                 $this->shipName = (string) $shipName;
00093         }
00094 
00095         // Getter for ship name
00096         public final function getShipName () {
00097                 return $this->shipName;
00098         }
00099 
00100         // Getter for ship instance
00101         public final function getShipInstance () {
00102                 return $this->shipInstance;
00103         }
00104 
00105         // Add detail to the contract
00106         public function addContractDetails ($shipPart, $parentPart, array $dataArray) {
00107                 // Secure strings
00108                 $shipPart   = (string) $shipPart;
00109                 $parentPart = (string) $parentPart;
00110 
00111                 // Debug message
00112                 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Das Schiffsteil <strong>%s</strong> wird zusammen mit dem Konstruktionsteil <strong>%s</strong> in den Bauvertrag aufgenommen.",
00113                         __CLASS__,
00114                         __LINE__,
00115                         $shipPart,
00116                         $parentPart
00117                 ));
00118 
00119                 // Initialize the instance (shall not be done within dynamic part)
00120                 $partInstance = null;
00121 
00122                 // Try to get an instance for this ship part
00123                 try {
00124                         $partInstance = ObjectFactory::createObjectByName($shipPart, $dataArray);
00125                 } catch (DimNotFoundInArrayException $e) {
00126                         $this->debugOutput(sprintf("[main:] Die <strong>%s</strong> konnte nicht vervollst&auml;ndigt werden. Grund: <strong>%s</strong><br />",
00127                                 $this->getShipInstance()->getShipName(),
00128                                 $e->getMessage()
00129                         ));
00130 
00131                         // Debug message
00132                         if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Versuche ein Schiffsteil in den Bauvertrag aufzunehmen.",
00133                                 __CLASS__,
00134                                 __LINE__
00135                         ));
00136 
00137                         // Is this ship part constructable?
00138                         if (!$partInstance instanceof ConstructableShipPart) {
00139                                 // Ship part not constructable!
00140                                 throw new ShipPartNotConstructableException(array($shipPart), self::EXCEPTION_NOT_CONSTRUCTABLE);
00141                         } elseif ($this->getShipInstance()->createShipPart($partInstance, $parentPart) === false) {
00142                                 // Schiff konnte nicht gebaut werden!
00143                                 throw new ShipNotConstructedException(sprintf("Das Schiff <strong>%s</strong> konnte wegen eines Fehlers nicht gebaut werden. Siehe obere Meldungen.",
00144                                         $this->getShipInstance()->getShipName()
00145                                 ));
00146                         }
00147                 } catch (ClassNotFoundException $e) {
00148                         // Throw it again...
00149                         throw new ClassNotFoundException($e->getMessage(), $e->getCode());
00150                 }
00151 
00152                 // Get price for this item
00153                 $price = $this->getMerchantInstance()->getPriceFromList($partInstance);
00154 
00155                 // Add price
00156                 $partInstance->setPrice($price);
00157         }
00158 
00159         // Setter for contract partner
00160         public final function setContractPartner (ContractPartner $partnerInstance) {
00161                 $this->contractPartner = $partnerInstance;
00162         }
00163 
00164         // Getter for contract partner
00165         public final function getContractPartner () {
00166                 return $this->contractPartner;
00167         }
00168 
00169         // Setter for contract party
00170         public final function setContractParty (ContractPartner $partyInstance) {
00171                 $this->contractParty = $partyInstance;
00172         }
00173 
00174         // Getter for contract party
00175         public final function getContractParty () {
00176                 return $this->contractParty;
00177         }
00178 
00179         // Setter for signature
00180         public final function setSigned ($signed) {
00181                 $this->signed = (boolean) $signed;
00182         }
00183 
00184         // Getter for signature
00185         public function isSigned () {
00186                 return $this->signed;
00187         }
00188 
00189         // Sign the contract
00190         public function signContract (ContractPartner $partnerInstance, ContractPartner $partyInstance) {
00191                 // Is this contract already signed?
00192                 if ($this->isSigned()) {
00193                         // Throw an exception
00194                         throw new ContractAllreadySignedException(array($this, $this->getContractPartner(), $this->getContractParty()), self::EXCEPTION_CONTRACT_ALREADY_SIGNED);
00195                 }
00196 
00197                 // Is the first contract partner still the same?
00198                 if ($partnerInstance->equals($this->getContractPartner())) {
00199                         // Set contract party (other partner is already set)
00200                         $this->setContractParty($partyInstance);
00201 
00202                         // Finally sign it
00203                         $this->setSigned(true);
00204                 } else {
00205                         // Throw an exception
00206                         throw new ContractPartnerMismatchException(array($this, $this->getContractPartner(), $partyInstance), self::EXCEPTION_CONTRACT_PARTNER_MISMATCH);
00207                 }
00208 
00209                 // Debug message
00210                 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) {
00211                         if ($partnerInstance->equals($partyInstance)) {
00212                                 // With itself
00213                                 $this->debugOutput(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> stimmt einem Bauvertrag &uuml;ber das <strong>%s</strong> <em><strong>%s</strong></em> zu.",
00214                                         __CLASS__,
00215                                         __LINE__,
00216                                         $partnerInstance->getObjectDescription(),
00217                                         $partnerInstance->getCompanyName(),
00218                                         $this->getShipInstance()->getObjectDescription(),
00219                                         $this->getShipInstance()->getShipName()
00220                                 ));
00221                         } else {
00222                                 // Other contract party
00223                                 $this->debugOutput(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> geht mit der <strong>%s</strong> <em><strong>%s</strong></em> einen Bauvertrag &uuml;ber das <strong>%s</strong> <em><strong>%s</strong></em> ein.",
00224                                         __CLASS__,
00225                                         __LINE__,
00226                                         $partnerInstance->getObjectDescription(),
00227                                         $partnerInstance->getCompanyName(),
00228                                         $partyInstance->getObjectDescription(),
00229                                         $partyInstance->getCompanyName(),
00230                                         $this->getShipInstance()->getObjectDescription(),
00231                                         $this->getShipInstance()->getShipName()
00232                                 ));
00233                         }
00234                 }
00235         }
00236 
00237         // Setter for merchant instance
00238         public final function setMerchantInstance (Merchant $merchantInstance) {
00239                 $this->merchantInstance = $merchantInstance;
00240         }
00241 
00242         // Getter for merchant instance
00243         public final function getMerchantInstance () {
00244                 return $this->merchantInstance;
00245         }
00246 
00247         // Getter for total price
00248         public final function getTotalPrice () {
00249                 // Get ship instance
00250                 $shipInstance = $this->getShipInstance();
00251 
00252                 // Is this a ship?
00253                 if (is_null($shipInstance)) {
00254                         // Opps! Empty partner instance?
00255                         throw new NullPointerException($shipInstance, self::EXCEPTION_IS_NULL_POINTER);
00256                 } elseif (!is_object($shipInstance)) {
00257                         // Not an object! ;-(
00258                         throw new NoObjectException($shipInstance, self::EXCEPTION_IS_NO_OBJECT);
00259                 } elseif (!$shipInstance instanceof ConstructableShip) {
00260                         // Does not have the required feature (method)
00261                         throw new ShipIsInvalidException(array($shipInstance), self::EXCEPTION_INVALID_SHIP_INSTANCE);
00262                 }
00263 
00264                 // Get the structure array
00265                 $struct = $shipInstance->getStructuresArray();
00266 
00267                 // Is this a ship?
00268                 if (is_null($struct)) {
00269                         // Opps! Empty partner instance?
00270                         throw new EmptyStructuresListException($this, self::EXCEPTION_EMPTY_STRUCTURES_ARRAY);
00271                 }
00272 
00273                 // Init total price
00274                 $totalPrice = 0;
00275 
00276                 // Iterate through the list
00277                 for ($iter = $struct->getIterator(); $iter->valid(); $iter->next()) {
00278                         // Get item
00279                         $item = $iter->current();
00280 
00281                         // Is this a ship?
00282                         if (is_null($item)) {
00283                                 // Opps! Empty partner instance?
00284                                 throw new NullPointerException($item, self::EXCEPTION_IS_NULL_POINTER);
00285                         } elseif (!is_object($item)) {
00286                                 // Not an object! ;-(
00287                                 throw new NoObjectException($item, self::EXCEPTION_IS_NO_OBJECT);
00288                         } elseif (!$item instanceof BaseSimulator) {
00289                                 // Does not have the required feature (method)
00290                                 throw new MissingMethodException(array($item, 'getPartInstance'), self::EXCEPTION_MISSING_METHOD);
00291                         }
00292 
00293                         // Get part instance
00294                         $part = $item->getPartInstance();
00295 
00296                         // Is this a ship?
00297                         if (is_null($part)) {
00298                                 // Opps! Empty partner instance?
00299                                 throw new NullPointerException($part, self::EXCEPTION_IS_NULL_POINTER);
00300                         } elseif (!is_object($part)) {
00301                                 // Not an object! ;-(
00302                                 throw new NoObjectException($part, self::EXCEPTION_IS_NO_OBJECT);
00303                         } elseif (!method_exists($part, 'getPrice')) {
00304                                 // Does not have the required feature (method)
00305                                 throw new MissingMethodException(array($part, 'getPrice'), self::EXCEPTION_MISSING_METHOD);
00306                         }
00307 
00308                         // Get price for one item
00309                         $price = $part->getPrice();
00310 
00311                         // Is there numCabin() available?
00312                         if (method_exists($item, 'getNumCabin')) {
00313                                 // Get total cabin and multiply it with the price
00314                                 $price = $price * $item->getNumCabin();
00315                         }
00316 
00317                         // Add price to total price
00318                         $totalPrice += $price;
00319                 }
00320 
00321                 // Total price calculated?
00322                 if ($totalPrice === 0) {
00323                         // Throw exception
00324                         throw new TotalPriceNotCalculatedException($this, self::EXCEPTION_TOTAL_PRICE_NOT_CALCULATED);
00325                 }
00326 
00327                 // Return total price
00328                 return $totalPrice;
00329         }
00330 }
00331 
00332 // [EOF]
00333 ?>

Generated on Mon Dec 8 01:06:44 2008 for Ship-Simulator by  doxygen 1.5.6